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.
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:
That will enable you to do
www.mysite.com/Ex/GetParameterYou also need to change you Index action on you ExController to:
to get the Modelbinder to bind postId correctly.
That will then in turn call the action Index passing
GetParameteras the postIdHope this helps!