Tag helpers do not generate my url correctly

I have the following routes installed:

app.UseMvc(routes => { routes.MapRoute( name: "admin", template: "{controller=Home}/{action=Index}/{id?}", defaults: new {Area = "Admin"}, constraints: new {HostConstraint = new MyConstraint()}); routes.MapRoute( name: "admin-rep", template: "Rep/{controller=Home}/{action=Index}/{id?}", defaults: new { Area = "" }, constraints: new { HostConstraint = new MyConstraint() }); routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); 

MyConstraint in this case always returns true .

  • If I try to access the "/" route, I will return to the "Admin", "Home of the controller", "Action index" areas. Things are good.
  • If I try to access "/ rep", then I will get to the root area, controller home, action index. OK. The problem is that this view has a link linking to another action in my HomeController (from the root area). I expected the link to point to / rep / home / action 2, but the generated route is / home / action2. Does this sound like a wrong match when creating urls?
 <a asp-action="Action">Action</a> @Html.ActionLink("Action", "Action") 
+5
source share
2 answers

reorder your routes, first write admin-rep and then admin

 app.UseMvc(routes => { routes.MapRoute( name: "admin-rep", template: "Rep/{controller=Home}/{action=Index}/{id?}", defaults: new { Area = "" }, constraints: new { HostConstraint = new MyConstraint() }); routes.MapRoute( name: "admin", template: "{controller=Home}/{action=Index}/{id?}", defaults: new {Area = "Admin"}, constraints: new {HostConstraint = new MyConstraint()}); routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); 
+3
source

You used Area , but I think you did not specify the asp-area attribute in the tag helper, and this causes this problem. For more information on setting up asp-area see the link below:

Asp.Net MVC Core 1.0 - Binding Tag Helper with Empty Area

+1
source

Source: https://habr.com/ru/post/1258897/


All Articles