Sunday, May 17, 2015

Catch all parameters in routing

Sometimes it is needed to catch all parameter from url. If so , how we can implement routing to catch all parameter ?

The implementation is very simple, Just add “*” before the parameter name. Have a look on blowentry in route table.

You can see that “CatchAll” entry has “*” followed by parameter name. So this entry can able to catch all parameters from url.

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "CatchAll",
                url: "{controller}/{action}/{*value}",
                defaults: new { controller = "Home", action = "Index", value = UrlParameter.Optional }

            );
        }

Here is my controller.

public class DummyController : Controller
{
        public void Index(string value)
        {
           
        }
    }



We are invoking the Index action by this url http://localhost:61684/Dummy/Index/a/a/c and we are seeing that Index has invoked.


No comments:

Post a Comment