2

I've got an Area in my ASP.NET MVC4 app that I register in Global.asax. Anytime I try to access the default route at:

whateverurl.tld/Area

ASP.NET MVC4 adds a trailing slash to it such that it looks like:

whateverurl.tld/Area/

and Visual Studio 2012's embedded IIS server throws back a 404.

However, if I browse to 'whateverurl.tld/Area/Index', it shows up. I've got another route that uses EXACTLY the same default route (sans the controller name), and when I access that route in my browser, it goes to 'whateverurl.tld/OtherArea' WITHOUT the trailing slash. As expected, without the trailing slash, the index for the controller is shown.

This is driving me absolutely insane. The configuration for each area is nearly identical. I can come up with some kind of workaround, but I really REALLY need to know what's causing this behavior to avoid it in the future.

Area 1 (Not Working):

        context.MapRoute(
            "Area1_default",
            "Area1/{action}/{id}",
            new { controller = "Area1", action = "Index", id = UrlParameter.Optional }
        );

Area 2 (Working):

        context.MapRoute(
            "Area2_default",
            "Area2/{action}/{id}",
            new { controller = "Area2", action = "Index", id = UrlParameter.Optional }
        );

Global.asax.cs:

        Area1Registration aReg1 = new Area1Registration();
        AreaRegistrationContext a1c = new AreaRegistrationContext(aReg1.AreaName, RouteTable.Routes);
        aReg1.RegisterArea(a1c);

        Area2Registration aReg2 = new Area2Registration();
        AreaRegistrationContext a2c = new AreaRegistrationContext(aReg2.AreaName, RouteTable.Routes);
        aReg2.RegisterArea(a2c);

UPDATE: I used a route debugger to see what route it matches. It matches no routes. Not particularly sure what the hell is going on. I can't get the route debug info to display because apparently the '/' is getting added before the route matching is done.

4
  • How are you registering these routes? Commented Mar 26, 2014 at 21:27
  • Just updated, that should provide more information Commented Mar 26, 2014 at 21:29
  • @NewToMS I don't know why you get this slash but just for the sake of trying, if you want in context.MapRoute replace "Area1/{action}/{id}", with "Area1/{controller}/{action}/{id}", Commented Mar 26, 2014 at 22:12
  • Do you have a default URL set in your project properties? Commented Mar 27, 2014 at 2:00

2 Answers 2

3

Friends,

I had a directory in my web root with the same name as the area. Solved my problem by renaming it. That explains the trailing slash - / means it was looking at a directory.

Thank you!

Sign up to request clarification or add additional context in comments.

1 Comment

Can confirm this is what is causing the issue. Also tried renaming the folder and it was working fine afterwards - no more trailing slash redirect. It seems IIS prefers to point to a folder rather than match a route in the RouteTable first. Don't want to rename my folders though - is there a way to "ignore" folders in application root?
1

This should fix it.

RouteTable.Routes.AppendTrailingSlash = false;

Put that in your Global.asax.

1 Comment

It doesn't fix it. If you have a folder within the application root that has a same name as controller it will always try to point to the folder. Think AppendTrailingSlash defaults to false anyway.

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.