Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I route a URL with a querystring in ASP.NET MVC?

I'm trying to setup a custom route in MVC to take a URL from another system in the following format:

../ABC/ABC01?Key=123&Group=456

The 01 after the second ABC is a step number this will change and the Key and Group parameters will change. I need to route this to one action in a controller with the step number key and group as paramters. I've attempted the following code however it throws an exception:

Code:

routes.MapRoute(     "OpenCase",      "ABC/ABC{stepNo}?Key={key}&Group={group}",     new {controller = "ABC1", action = "OpenCase"} ); 

Exception:

`The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character.` 
like image 724
Jason Underhill Avatar asked Aug 04 '11 13:08

Jason Underhill


People also ask

How do I pass QueryString?

To pass in parameter values, simply append them to the query string at the end of the base URL. In the above example, the view parameter script name is viewParameter1.

How can use route in ASP.NET MVC?

The ASP.NET Routing module is responsible for mapping incoming browser requests to particular MVC controller actions. By the end of this tutorial, you will understand how the standard route table maps requests to controller actions.

What is request QueryString in asp net?

Request. QueryString["pID"]; Here Request is a object that retrieves the values that the client browser passed to the server during an HTTP request and QueryString is a collection is used to retrieve the variable values in the HTTP query string.


1 Answers

You cannot include the query string in the route. Try with a route like this:

routes.MapRoute("OpenCase", "ABC/ABC{stepNo}",    new { controller = "ABC1", action = "OpenCase" }); 

Then, on your controller add a method like this:

public class ABC1 : Controller {     public ActionResult OpenCase(string stepno, string key, string group)     {         // do stuff here         return View();     }         } 

ASP.NET MVC will automatically map the query string parameters to the parameters in the method in the controller.

like image 155
Hector Correa Avatar answered Oct 06 '22 23:10

Hector Correa