How do I access a route parameter in my ASP.NET MV

2019-02-04 11:58发布

问题:

I have an URL like this /home/action/id

How can I access this id in view?

回答1:

This should work in your view:

<%= this.ViewContext.RouteData.Values["id"] %>

(assuming the route parameter is named "id")



回答2:

you can pass it in through viewData;

In your Controller:

public ActionResult Index(string id)
{
    ViewData["Name"] = Server.UrlEncode(id);
    return View();
}

In your View:

<h1><%= ViewData["Name"] %></h1>