7

My app is mainly an ASP.NET app that I'm adding an MVC section to it.

My Default.aspx (no codebehind) page has a simple Response.Redirect to a StartPage.aspx page but for some reason MVC is taking over and I'm not getting to the StartPage.aspx page. Instead I get routed over to my first and only MVC section which is a registered route that I've registered in the global.asax.cs page (Albums).

Is there a way to tell MVC to leave my requests to the root "/" to be my IIS 7 default document...in this case Default.aspx?

This is what is in my RegisterRoutes:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Albums","{controller}/{action}/{id}",
    new { controller = "Albums", action = "Index", id = "" });
2
  • Do you want to break MVC(Model-View-Controller) rules? I think every view must be contained under view folder. Commented Apr 30, 2009 at 4:40
  • 1
    it's a classic asp.net app. i guess i can call it a legacy app now ;-) so everything remains the same. i'm just starting to add MVC and the default page isn't one of them. Commented Apr 30, 2009 at 13:18

3 Answers 3

7

If you remove the default controller from your second route there, it won't match against "/" anymore and Routing will ignore requests for "/", leaving them for the usual ASP.Net pipeline to handle

So, change your routes to:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
routes.MapRoute("Albums","{controller}/{action}/{id}",
     new { action = "Index", id = "" });

That should solve your problem!

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

2 Comments

Your suggestion worked but I'm wondering if it is better to ignore the default.aspx page like Alconja mentions? In the future as I create more MVC routes I won't be able to specify a default controller or am I misunderstanding what specifying the {controller} means? I figured it was specific to "Albums" but I'm not sure. Thanks.
I'm not sure if Alconja's solution will actually work in the "/" case. Since the URL arriving is "/" rather than "/Default.aspx". It depends on when the Default Document is applied. The only reason to specify a default controller value is so that requests to "/" get routed to that controller, but if you want your Default.aspx page to handle requests to "/", you should NOT have a default controller
1

The default.aspx page is being served by IIS because it is the default document. MVC would let the default.aspx page handle the request, if it realized that the request was for default.aspx (e.g. "http://foo.com/default.aspx"). It doesn't relize that though in this scenario ("http://foo.com") so you could add this before the default route to achieve what you are after

// ignore "/"    
routes.IgnoreRoute("");

// default route
routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", 
                      id = UrlParameter.Optional } // Parameter defaults
            );

Comments

0

You could tell the MVC to ignore the Default.aspx like this:

routes.IgnoreRoute("Default.aspx");

Comments

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.