Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Inside area: relative url when no area defined in link #4319

Closed
nooxnet opened this issue Mar 20, 2016 · 11 comments
Closed

Inside area: relative url when no area defined in link #4319

nooxnet opened this issue Mar 20, 2016 · 11 comments

Comments

@nooxnet
Copy link

nooxnet commented Mar 20, 2016

I'm not sure if this is intended behavior or I've missed something when I moved from RC1 to RC2 Nightly yesterday:

I have this url inside _Layout.cshtml (under the main Views/Shared/ directory):

<a asp-controller="Home" asp-action="Index">Home</a>

With RC1 this link has always been rendered as

<a href="/">Home</a>

Now with RC2 it is rendered as "/" only when I'm on a page in the root directory But as soon as I'm on a page in the /Admin directory (area) it's rendered as:

<a href="/Admin/Home">Home</a>

This behavior can be fixed by adding an empty area:

<a asp-controller="Home" asp-action="Index" asp-area="">Home</a>

This also applies to

@Html.ActionLink("Home", actionName: "Index", controllerName: "Home")
// fixed:
@Html.ActionLink("Home", actionName: "Index", controllerName: "Home", routeValues: new { area = "" }, htmlAttributes: null)

My routes:

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "areaDefault",
        template: "{area:exists}/{controller=AreaRoot}/{action=Index}/{id:int?}" 
        );
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});

Is this a bug? Is there a global setting? If this is intended then one has to add asp-area="" to links on root pages as soon as an area is added.

@nooxnet nooxnet changed the title Inside area: relative url when no area defined Inside area: relative url when no area defined in link Mar 20, 2016
@pranavkm
Copy link
Contributor

Seems like a result of #4013. cc @javiercn

@javiercn
Copy link
Member

I believe the behavior you are seeing is correct in both cases. I don't think #4013 has anything to do with it.

When you are inside of an area, and try to generate a link outside of the area you need to specify the area explicitly, (the other area you want to generate the link to or null or empty string in case you want to generate a link to an action outside of any area).

What is happening in your situation is that you are inside of an Admin area, when you try to generate a link, the area is implicitly defined in the route values (as you hit an action within the area). When the link generation algorithm goes through the list of routes, it tries the first one (the one with the area and it matches it.

@rynowak
Copy link
Member

rynowak commented Mar 21, 2016

This is an intended change in behavior: aspnet/Announcements#120

@nooxnet
Copy link
Author

nooxnet commented Mar 21, 2016

Yes, it's a consistent behavior. If you don't specify a controller the current controller is used. If you don't specify an area, the current area is used.

But I assume that many will stumble over this when they have some sort of menu and introduce an area later on. They will end up with broken links. It's probably a more common scenario than reusing an area-relative link in different areas.

Maybe 'asp-area=""' should be added to menu links in samples and templates?

@danroth27
Copy link
Member

No plans to make a change in behavior here.

@ermithun
Copy link

ermithun commented May 30, 2016

<form asp-action="Register" asp-controller="Account" asp-route-area="Global"> in RC1
It gives

<form method="post" action="Global/Account/Register/">

But RC2
<form asp-action="Register" asp-controller="Account" asp-route-area="Global">
gives
<form method="post" action="/Account/Register?area=Global">
Need help

@Muchiachio
Copy link
Contributor

What's your route configuration?

@ermithun
Copy link

@Muchiachio
My route config
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");

            routes.MapRoute(name: "areaRoute",
template: "{area:exists}/{controller=Home}/{action=Index}");
        });
    }

@Muchiachio
Copy link
Contributor

Muchiachio commented May 30, 2016

Try swapping them around, so that area route would be the first to get evaluated.

@ermithun
Copy link

@Muchiachio Thanx

@rynowak
Copy link
Member

rynowak commented Jun 3, 2016

Please see the announcement post here for explanation #3665

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants