Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default.aspx not getting executed in ASP.NET project with MVC sections

Tags:

asp.net-mvc

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 = "" });
like image 982
Mouffette Avatar asked Apr 30 '09 03:04

Mouffette


2 Answers

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!

like image 66
Andrew Stanton-Nurse Avatar answered Sep 17 '22 08:09

Andrew Stanton-Nurse


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
            );
like image 30
Jon Kragh Avatar answered Sep 18 '22 08:09

Jon Kragh