41

I am creating an ASP.NET MVC 5 application and I have some issues with routing. We are using the attribute Route to map our routes in the web application. I have the following action:

[Route("{type}/{library}/{version}/{file?}/{renew?}")]
public ActionResult Index(EFileType type, 
                          string library, 
                          string version, 
                          string file = null, 
                          ECacheType renew = ECacheType.cache)
{
 // code...
}

We only can access this URL if we pass the slash char / in the end of url, like this:

type/lib/version/file/cache/

It works fine but does not work without /, I get a 404 not found error, like this

type/lib/version/file/cache

or this (without optional parameters):

type/lib/version

I would like to access with or without / char at the end of url. My two last parameters are optional.

My RouteConfig.cs is like this:

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

        routes.MapMvcAttributeRoutes();
    }
}

How can I solve it? Make the slash / be optional too?

10
  • By "does not work" you mean you get 404 Not Found?
    – haim770
    Commented Jul 10, 2014 at 13:59
  • Yes! 404 error, if I add a breakpoint, it just does not hit on the breakpoint! Commented Jul 10, 2014 at 14:02
  • Does the application hosted as a Virtual Directory?
    – haim770
    Commented Jul 10, 2014 at 14:03
  • Starting from Visual Studio, it's running on the IIS Express! Commented Jul 10, 2014 at 14:03
  • 2
    If {file} is not provided, but {renew} is, how is it supposed to know which one you provided since they are both optional? Commented Jul 10, 2014 at 14:22

2 Answers 2

54

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/1
http://localhost:52392/2/lib1/ver1/file1
http://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...

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.