aspnet / Routing

[Archived] Middleware for routing requests to application logic. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
272 stars 123 forks source link

Problem with building URLs with Areas in .NET Core 2.1 #623

Closed Eilon closed 6 years ago

Eilon commented 6 years ago

From @piecho1991 on July 17, 2018 13:48

I have problem with URL-s in ASP.NET Core 2.1 Web Application.

I have used code like below:

<a asp-area="Area" asp-controller="Controller" asp-action="List">nasihdfgasih</a>

and Razor is generating wrong URL not like http://example.com/Area/Controller/List but http://example.com/Controller/List?area=Area.

My routes configuration:

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

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

Copied from original issue: dotnet/core#1796

mkArtakMSFT commented 6 years ago

Thanks for contacting us, @piecho1991. @kichalla, can you please look into this? Thanks!

kichalla commented 6 years ago

@piecho1991 You need to reorder the routes in your configuration. In general, routes with more number of segments (in your case 4 vs. 3) should be before the one with less number of segments

mkArtakMSFT commented 6 years ago

Thanks for contacting us. We believe that the question you've raised have been answered. If you still feel a need to continue the discussion, feel free to reopen it and add your comments.

Apower952 commented 6 years ago

Hi, I believe I am experiencing the same issue.

My routes:

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

    routes.MapRoute(
    name: "default",
    template: "{controller=Home}/{action=Index}/{id?}");
});

From what I can tell this issue is related to having Identity being implemented within the project. To test this I created a new application with no authentication and areas/routes work great no problems.

Creating a new project with individual user accounts being added as the authentication results in the routes no longer operating correctly. The result being a URL that looks like this - https://localhost:44396/Admin?area=Admin

neesinch commented 6 years ago

Even i am f

Hi, I believe I am experiencing the same issue.

My routes:

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

  routes.MapRoute(
  name: "default",
  template: "{controller=Home}/{action=Index}/{id?}");
});

From what I can tell this issue is related to having Identity being implemented within the project. To test this I created a new application with no authentication and areas/routes work great no problems.

Creating a new project with individual user accounts being added as the authentication results in the routes no longer operating correctly. The result being a URL that looks like this - https://localhost:44396/Admin?area=Admin

Even i am facing exactly same issue, on a sample application url works fine, but as son as I add authentication its breaking, unable to figure out whats going wrong. Just stuck up in our migration to .net core process.

neesinch commented 6 years ago

Cshtml code <a href="@Url.Action("Index", "Story", new { area = "Bio" })" ></a>

html rendered code <a href="/Bio?area=Bio"> </a>

Routes:

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    "Bio_default",
                    "Bio/{controller}/{action}/{id?}",
                    new { controller = "Story", action = "Index" }
                   );
                routes.MapRoute(
                    "default_route",
                    "{controller}/{action}/{id?}",
                    new { controller = "Home", action = "Index" }
                );
                       });

Even changing the area route to

                routes.MapRoute(
                    "Bio_default",
                    "{area:exists}/{controller}/{action}/{id?}",
                    new { controller = "Story", action = "Index" }
                );

isn't changing the behavior. Can someone suggest what could be wrong here. I am banging my head right now. :(

Apower952 commented 6 years ago

Hi @neesinch,

I had given up on looking for a fix for this. I had a quick look there there this morning and found some documention for core 2.1 - https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/areas?view=aspnetcore-2.1

It looks like you're meant to mark the areas controllers with an area attribte and provide the area name. See my example below. "Admin" is the name of my area. So I have;

I then setup my routes like so;

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

                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

Keep in mind that the area route defination has to come first. Then this is what my link off to the area looks like <li><a asp-area="Admin" asp-controller="AdminPanel" asp-action="Index">Admin Panel</a></li>

I would suspect that your <a href="@Url.Action("Index", "Story", new { area = "Bio" })" ></a> would also work.

demonguru18 commented 6 years ago

Please don't forget to add the attribute [Area("Admin")] public class HomeController : Controller { public IActionResult Index() { return View(); } }

rynowak commented 6 years ago

Hi, it looks like you are posting on a closed issue/PR/commit!

We're very likely to lose track of your bug/feedback/question unless you:

  1. Open a new issue
  2. Explain very clearly what you need help with
  3. If you think you have found a bug, include detailed repro steps so that we can investigate the problem

Thanks!