Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set up a route for the home page of an ASP.NET MVC site?

I'm working with an ASP.NET MVC site which will use a CMS controller for all pages of the site except for the home page. Here's the idea:

Home controller:

  • www.site.com
  • www.site.com/default.aspx

CMS Controller:

  • www.site.com/about
  • www.site.com/agenda/schedule
  • www.site.com/monkey/eats/spaghetti
  • (pretty much anything else)

This page lists some options on how to set up a default page routing:

  1. Leave Default.aspx unrouted and unredirected as the entry point to your application - with static links that take your users into the MVC portion of the app (or other static content).
  2. Redirect Default.aspx in the code behind, either using the Page_Load event handler code, or use Response.Redirect("~/home") to send them to the Home controller (although this is a round-trip redirect).
  3. Rename or delete Default.aspx. Despite the warning in the markup that says that default.aspx is required to ensure that ASP.NET MVC is activated by IIS when a user makes a "/" request... it's not actually needed in either the VS dev server, or IIS7. The default request will remain an application root request "/" and will be caught by the default route and sent to the home controller.

I guess one other option is to just use one controller with some logic that detects the home page case, but that seems to be fighting the concept.

How do you recommend setting up a specific route for the site home page?

like image 333
Jon Galloway Avatar asked Apr 08 '09 17:04

Jon Galloway


2 Answers

www.site.com can be handled by an root map route

routes.MapRoute(
    "Root",
    "",
    new { controller = "Home", action = "Index", id = "" }
);

Put the following in page load of Default.aspx

HttpContext.Current.RewritePath(Request.ApplicationPath, false);
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest(HttpContext.Current);

This rewrites the request to root and handled by the map route above.

BTW, you can actually find the code from the MVC template project.

like image 72
Canton Avatar answered Oct 06 '22 00:10

Canton


If hosting on IIS7 integrated mode, I suggest just getting rid of default.aspx. As I understand it, it's only necessary for activation on IIS6 and IIS7 classic mode.

like image 21
Kevin Dente Avatar answered Oct 06 '22 00:10

Kevin Dente