开发者

ASP.NET MVC 1 Route parameter question

I need to have a url that passes an additional parameter in the querystring.

ie. The default "{controller}.mvc/{action}/{id}" works in most cases but I need to add an additional querystring parameter for a couple of pages.

Do I add something to Global.asax.cs to cater for this ?

My concern is that a list page would be something like Forms.mvc/Survey

and the details page would be something like Forms.mvc/Survey/1

But for both of those pages I need to add the additional querystring parameter (TypeId).

I know I c开发者_运维知识库an just go old school and do the "?TypeId=..." but that seems wrong in the world of MVC. I am using MVC 1.

So my questions are - how to add the additional query string parameter using routing, and how to not get the list screen confused with the detail screen after adding the new parameter.

Is it as simple as something like:

routes.MapDomains("{controller}.mvc/{action}/{typeid}/{id}", "DomainX", new[] { "Forms" });

The problem may be that not all Forms domain pages may use this typeid..

Or should I add a routes.MapRoute before the MapDomains to catch the two "sections" that use this typeid?

Sorry if this is a really basic question - still new to MVC!! :)

Thanks!!


Not wrong, It will work if you add querystring parameters the classic way but it wont be RESTFul and you wont be able to "Generate" action links...because routing constraints can be used both ways as ScottGu blog about URL routing: http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx

ASP.NET MVC 1 Route parameter question

You can pay a visit to this blog about Create Custom Route Constraints


You don't need to do anything to the routes in order to take advantage of using querystrings to pass data to a controller. All you need to do is make sure the querystring name is specified as a variable in the controller method that the route will match.

So if you have a URL of

"Forms.mvc/Survey/1?TypeId=1"

You can match the route with the querystring as this:

public ActionResult Survey(int? id, int TypeId) {...}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜