Create route that will move the query string to part of the url
I'd like to change my routes so that instead of开发者_如何学JAVA having:
/Users/Edit?UserID=1
I can do
/Users/Edit/1
How can I create a custom route to do that?
Also, can someone direct me to a good tutorial on routes? I don't wanna create a post every time I have a simple problem with routes.
Actually http://localhost/Users/Edit/1 can do the what you want without defining a new route. The default route structure is http://localhost/controllerName/actionMethodName/id.
So if you just use "id" instead of "UserID" in your action method. The number in the URL would be assigned to the parameter on action.
public ActionResult Edit(int id)
Here's a few lesson on routing. Take a look at them. It's a quite easy thing to understand.
- http://www.asp.net/mvc/tutorials/asp-net-mvc-routing-overview-cs
- http://www.asp.net/mvc/tutorials/creating-custom-routes-cs
- http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx
You will want to use the htaccess file. You put in a regular expression which will extract pieces of the file path as if it is part of the query string.
This is a link I googled it may do the trick.
You will want to do something like this:
routes.MapRoute(
"MyRoute",
"{controller}/{action}/{UserId}",
new { controller = "DefaultController",
action = "DefaultAction",
UserId = UrlParameter.Optional }
);
精彩评论