Did Microsoft change the way to pass string as parameter in ASP.NET MVC 3?
I have this simple question. Previously, when I wanted to call a controller method with only one parameter, I could do it simply calling /ControllerName/Method/Parameter
, whatever type this parameter was. Now, I did the same thing with an integer value withou开发者_运维问答t problems, but with a string it didn't work. Am I going nuts or Microsoft actually changed this?
The default route that you'll find in Global.aspx.cs is still the following:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
So your "parameter" is the {id}
in the example above, presumably a number as IDs tend to be. Get to know your routes, they're fun! Linkage: http://www.asp.net/mvc/tutorials/asp-net-mvc-routing-overview-cs
I'm imagining your actions that work for int
s look something like this:
public ActionResult Index(int id)
If you want to accept a string parameter instead of an integer and have it be part of the (default) route, it also would need to be named id
in the method signature, like so:
public ActionResult Index(string id)
If you had an action with a signature like this:
public ActionResult Post(string slug)
Then with the default route slug would only have a value if you had a querystring (get
) or form (post
) value with the key slug
. A route that would match the above action and have the slug
parameter be populated (assuming it was a method of the BlogController
controller) would be:
routes.MapRoute(
"BlogPost",
"post/{slug}",
new { controller = "Blog", action = "Post", slug = "" }
);
精彩评论