1

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 can 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!!

2 Answers 2

1

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 Link can be generated from URL constraints

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks - unfortunately the constraints won't help me as both of the parameters as ints, but good to know. Does that work in mvc 1 ? Also the scottgu link unfortunately gives a 404. Hopefully I can use the map route with the two parameters from the Custom Route Constraints article.
For sure you can put two int(s) in the route I can understand why not ... and you can an optional parameter ... Regarding the links, I revised them and they're working
0

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) {...}

1 Comment

Thanks thats what I meant by saying I knew I could go old school with the querystring parameter but didn't think it was ideal in the world of MVC and nice urls :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.