views:

132

answers:

2

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

How can I access this id in view?

+3  A: 

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>
Jack Marchetti
+6  A: 

This should work in your view:

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

(assuming the route parameter is named "id")

M4N
is this potentially vulnerable, since you aren't doing any check on the parameter?
Jack Marchetti
@Jack: yes, probably. It is not guaranteed that a view is always invoked by using the same route, so the value for "id" might be missing.
M4N