ASP.NET MVC routing

So far, I have managed to use the default routing that comes with ASP.NET MVC. Unfortunately, now that I am moving on to more complex routes, I am struggling to wrap my head around how to make this work.

The simple example I'm trying to get is specifying the path / User / {UserID} / Items to map to the function of the User controller elements. Can someone tell me what I'm doing wrong with my routing here?

routes.MapRoute("UserItems", "User/{UserID}/Items", 
                      new {controller = "User", action = "Items"});

And on my aspx page

Html.ActionLink("Items", "UserItems", new { UserID = 1 })
+3
source share
4 answers

Switching to the MVC Preview 4 code I have before me the overload for the Html.ActionLink () that you are using is:

public string ActionLink(string linkText, string actionName, object values);

Note that the second parameter is the name actionName, not the name of the route.

, :

Html.ActionLink("Items", "Items", new { UserID = 1 })

:

<a href="<%=Url.RouteUrl("UserItems", new { UserId = 1 })%>">Items</a>
+4

? URL- aspx, ? - . , , .

+1

, URL- , Phil Haack . .

, . , .

In addition (and this is a purely personal opinion), I would like to generate my links somewhere in the beginning of the page in lines, and then put these lines in my HTML. These are tiny overheads, but in my opinion, the code is much more readable. In addition, if you have or duplicate links, you should only generate them once.

I prefer to bet

<% string action = Url.RouteUrl("NamedRoute", new 
    { controller="User",
      action="Items",
      UserID=1});%>

and then write

<a href="<%=action%>">link</a>
0
source
Html.ActionLink("Items", "User", new { UserID = 1 })
0
source

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


All Articles