Routing optional parameters in ASP.NET MVC 5 Routing optional parameters in ASP.NET MVC 5 asp.net asp.net

Routing optional parameters in ASP.NET MVC 5


Maybe you should try to have your enums as integers instead?

This is how I did it

public enum ECacheType{    cache=1, none=2}public enum EFileType {    t1=1, t2=2}public class TestController{    [Route("{type}/{library}/{version}/{file?}/{renew?}")]    public ActionResult Index2(EFileType type,                              string library,                              string version,                              string file = null,                              ECacheType renew = ECacheType.cache)    {        return View("Index");    }}

And my routing file

public static void RegisterRoutes(RouteCollection routes){    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");    // To enable route attribute in controllers    routes.MapMvcAttributeRoutes();    routes.MapRoute(            name: "Default",            url: "{controller}/{action}/{id}",            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });}

I can then make calls like

http://localhost:52392/2/lib1/ver1/file1/1http://localhost:52392/2/lib1/ver1/file1http://localhost:52392/2/lib1/ver1

or

http://localhost:52392/2/lib1/ver1/file1/1/http://localhost:52392/2/lib1/ver1/file1/http://localhost:52392/2/lib1/ver1/

and it works fine...


//its working with mvc5[Route("Projects/{Id}/{Title}")]public ActionResult Index(long Id, string Title){    return view();}