1

My problem is that when i am not in my "Admin" area the link shows up as "/blog" however when i am in my "Admin" area the link turns to "/admin/blog".

I have an "Admin" area and i have specified a layout page in a viewstart file in the "Admin" area View section. Markup below:

 Layout = "~/Views/Shared/_Layout.cshtml";

The markup of my link looks like this (this link is in my _Layout.cshtml view):

 <a asp-controller="Blog" asp-action="Index" class="nav-link"> Blog </a>

My routes look like this:

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

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

And my controller in my "Admin" area looks like this:

[Authorize(Roles = "Admin")]
[Area("Admin")]
public class ProductsController : Controller

I have tried adding asp-area="" so my a tag would look like :

<a asp-area="" asp-controller="Blog" asp-action="Index" class="nav-link"> Blog </a>

But this just leaves the href empty when looking at it through developer tools.

So my question is how can i remove the area name from the url?

I feel like it is something really easy but i cannot figure it out. I have looked at this thread but it is not in .net core so the solution does not work.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Daniaal
  • 882
  • 8
  • 16

1 Answers1

1

Your issue could be linked to your route definitions. Try combining the routes into a single UseMvc call and also placing the more specific area route first.

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

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

Following that, you could switch your tag to use asp-route="default" instead of using asp-controller="..." and asp-action="...".

Matt Brooks
  • 1,584
  • 1
  • 14
  • 27
  • wow i cannot believe i didnt try this. I had to use your solution combined with adding asp-area="" to the a tag and it worked. Thanks a thousand!! – Daniaal Apr 27 '18 at 09:12
  • No problem, I'm glad it worked. For what it is worth, I would recommend using the `asp-route="default"` named route approach, because you're effectively trying to target that specific `default` route. If you do that, it looks like you'll need to also specify `asp-route-controller="..."` and `asp-route-action="..."` as well. – Matt Brooks Apr 27 '18 at 12:36
  • @MattBrooks not works. If I used `asp-route="default"`, it always load home url only. – Prabhu Manoharan Sep 24 '18 at 08:27