Reputation: 2914
In my ASP.Net MVC app, I have the following controllers
HomeController
ExController
ExController has this method that takes string parameters:
public ActionResult Index(String id){....
With parameters, the page opens successfully as: mysite.com/Ex/Index/my-string-value
but I want it to take parameters as: mysite.com/Ex/my-string-value
Here are my routing entries:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Post",
"{controller}/{action}/{postId}",
new { controller = "Ex", action = "Index", postId="" }
);
What I need to do to send get parameters to ExController by typing mysite.com/Ex/GetParameter instead of mysite.com/Ex/Index/GetParameter. Please help.
Upvotes: 0
Views: 74
Reputation: 9676
First of all you need to define the Ex-route before the default route, otherwise the default will catch all. Second you can simply do this:
routes.MapRoute(
"Post",
"Ex/{postId}",
new { controller = "Ex", action = "Index", postId="" }
);
That will enable you to do www.mysite.com/Ex/GetParameter
You also need to change you Index action on you ExController to:
public ActionResult Index(String postId){....
to get the Modelbinder to bind postId correctly.
That will then in turn call the action Index passing GetParameter
as the postId
Hope this helps!
Upvotes: 3