Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Area links don't generate in asp.net-core-mvc

Following this guide I've been creating an Admin area: https://docs.asp.net/en/latest/mvc/controllers/areas.html

I can navigate to the admin pages by directly typing in the url, however I can't get links to generate

I've tried placing the following on the Admin index.cshtml:

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

Yet the all end up being non clickable anchors (no href)

In fact, when I view source, I actually still see the "asp-action" etc tags

<a asp-action="Index">Admin Index</a>

MVC doesn't seem to be processing the link helper tags?

Any idea what I've missed? Perhaps a configuration step somewhere?

Thanks

like image 851
mejobloggs Avatar asked Sep 17 '16 23:09

mejobloggs


People also ask

How to create area in ASP NET Core MVC?

How to create "Area" in ASP.net Core. As we are aware, there is no option to create area by right clicking on project folder. So if we want to create area in Asp.net Core MVC application, Create new folder and name it to area. Within this folder we can create another new folder and give it to any logical name.

What are areas in ASP NET MVC?

ASP.net MVC applications may have one or more areas. So, using Areas, we can divide our large application into smaller functional groups. The logical components such as Model, View and Controller are kept in different folders and MVC framework uses naming conventions to create relations between these components.

What is Anan area in ASP NET Core?

An area is effectively a structure inside an app. In an ASP.NET Core web project, logical components like Pages, Model, Controller, and View are kept in different folders. The ASP.NET Core runtime uses naming conventions to create the relationship between these components.

How do class namespaces affect routing in MVC?

Class namespaces have no effect on MVC's routing. The first two controllers are members of areas, and only match when their respective area name is provided by the area route value. The third controller isn't a member of any area, and can only match when no value for area is provided by routing.


2 Answers

Got it!

I needed to add a _ViewImports.cshtml to my Areas folder

_ViewImports.cshtml needs the following inside:

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

Odd how it wasn't mentioned in the tutorial

like image 82
mejobloggs Avatar answered Sep 28 '22 10:09

mejobloggs


Seeing as how this is the first result in searching for my issue and even though my issue was slightly different, I'll post my answer here.

My tags weren't working either. I had this:

<a href="@Url.Action("Queues", "Digium", new {Area = "Reporting"})">

and this:

<a asp-action="CurrentCalls" asp-controller="Digium" asp-area="Reporting">

And nothing was working... it wasn't recognizing the area and the links were coming out as

/Controller/Action?Area=MyArea

I was really confused because I had an area that was working just fine. That's when I realized that I had forgotten to put the Area annotation on the controller:

[Area("Marketing")]
public class LinkedInController : Controller

Don't forget your area annotation!

like image 22
Barry Franklin Avatar answered Sep 28 '22 11:09

Barry Franklin