MR.ABC
MR.ABC

Reputation: 4862

MVC4 ? (Questionmark) as / (Slash) for action/parameters separator

I've create a mvc4 application where i need a questionmark as action/parameter separator. This parameter is optional.

www.mydomain.com?myParam

config.Routes.MapHttpRoute(
  "DefaultRoute",
   routeTemplate: "{controller}/{action}?{*param}",
   defaults: new { controller = "Index", action = "Index", param = UrlParameter.Optional }
);

How the route should look like to accomplish this ?

Upvotes: 1

Views: 1600

Answers (1)

Aleksei Chepovoi
Aleksei Chepovoi

Reputation: 3955

You can build Your URL as usual (with params separated by ? and &). If You define any parameters separated by slash it means that You want Your URL to look more understandable.

If You want Your URL to be in a standard way just remove param definition:

config.Routes.MapHttpRoute(
"DefaultRoute",
routeTemplate: "{controller}/{action}",
defaults: new { controller = "Index", action = "Index"}
);

Now You can add as many params You want.
For instance Your URL can look like:

Index/Index?param1=something&param2=something etc

Updates:

public class IndexController : ApiController
{
    [HttpGet]
    public IEnumerable<MyClass> Index(DateTime startDate, DateTime endDate)
    {
        IEnumerable<MyClass> data = GetSomeData(startDate, endDate);
        return data;
    }
}

With the routing specified above You can write:

Index/Index?startDate=something&endDate=something

And the method will be invoked with these params.

Upvotes: 4

Related Questions