views:

3450

answers:

9
+22  Q: 

ASP.NET MVC on IIS6

Where can I find some good pointers on best practices for running ASP.NET MVC on IIS6?

I haven't seen any realistic options for web-hosts who provide IIS7-hosting yet. Mostly because I don't live in the U.S.

So I was wondering on how you best build applications in ASP.NET MVC and make it easily available to deploy on both IIS6 and IIS7. Keep in mind that this is for standard web-hosts, so there is no access to ISAPI-filters or special settings inside IIS6.

Are there anything else one should think about when developing ASP.NET MVC-applications to target IIS6? Any functions that doesn't work?

UPDATE: One of the bigger issues is the thing with routes. The pattern {controller}/{action} will work on IIS7, but not IIS6 which needs {controller}.mvc/{action}. So how do I make this transparent? Again, no ISAPI and no IIS-settings, please.

+2  A: 

With IIS6 you can do one of two things:

  1. Setup an ISAPI filter to map MVC URLs to ASP.NET
  2. Include an extension in the URL. For example: htp://localhost/Home.mvc

Since option 1 is not available on most web-hosts, you have to go for number 2.

Espo
+4  A: 

You don't have to live with that extension if you can install an ISAPI filter on the server.

Basically you route matched urls to the {controller}.mvc variety, then in ASP.NET you rewrite this url to remove .mvc -- doing this you don't have to define any extra routes or expose .mvc to your users.

I've written about this here: http://www.flux88.com/UsingASPNETMVCOnIIS6WithoutTheMVCExtension.aspx

and Steve Sanderson has a good post here as well: http://blog.codeville.net/2008/07/04/options-for-deploying-aspnet-mvc-to-iis-6/

Ben Scheirman
+4  A: 

Since you can't modify IIS settings to map .mvc to ASP.Net, you can use a different extension that's already mapped to ASP.Net. For example, you could use {controller}.ashx/{action} and it should work out of the box on IIS 6.

Sean Carpenter
Why was this voted down? It's a perfectly valid solution.
Richard Szalay
And the most apt one, given the OP. What the hell, people?
Chris
A: 

I have a detailed step by step guide, but it requires that you use isapi_rewrite. View it at: http://biasecurities.com/blog/2008/how-to-enable-pretty-urls-with-asp-net-mvc-and-iis6/

Jim Geurts
A: 

Jim, I read your article, it was that one (or maybe a similar one) that inspired me to ask this question. When you have a regular web-host, you don't have the flexibility of using ISAPI-filters, unfortunatly.

Seb Nilsson
+10  A: 

It took me a bit, but I figured out how to make the extensions work with IIS 6. First, you need to rework the base routing to include .aspx so that they will be routed through the ASP.NET ISAPI filter.

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

If you navigate to Home.aspx, for example, your site should be working fine. But Default.aspx and the default page address of http://[website]/ stop working correctly. So how is that fixed?

Well, you need to define a second route. Unfortunately, using Default.aspx as the route does not work properly:

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

So how do you get this to work? Well, this is where you need the original routing code:

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

When you do this, Default.aspx and http://[website]/ both start working again, I think because they become successfully mapped back to the Home controller. So the complete solution is:

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

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

    protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
    }
}

And your site should start working just fine under IIS 6. (At least it does on my PC.)

And as a bonus, if you are using Html.ActionLink() in your pages, you should not have to change any other line of code throughout the entire site. This method takes care of properly tagging on the .aspx extension to the controller.

remember if you ever decide to switch to IIS7 and remove .aspx then your SEO will all be messed up. you'll need to keep legacy routing so you can be 'found' in future
Simon_Weaver
creating a "legacy-style" route forwarding will solve that.
boj
That second route just needs to be a root route with a blank url value: routes.MapRoute("Root","", new {controller = "Home",action = "Index",id = ""});
Scott
+2  A: 

As mentioned in this blog post by Phil Hack, it is possible to setup extension-less URLs for ASP.NET MVC in IIS 6 using wildcard application mappings:

  1. In IIS 6, go to the Application Configuration Properties for your ASP.NET MVC web application.
  2. Click "Insert..." in the Wildcard application maps section.
  3. Set the Executable to the path of the aspnet_isapi.dll (on my machine this is c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll).
  4. Make sure NOT to check the "Verify that file exists" and click "OK".

However as also mentioned by Hack, there are some performance implications of doing this.

PropellerHead
+1  A: 

Url rewriting can help you to solve the problem. I've implemented solution allowing to deploy MVC application at any IIS version even when virtual hosting is used. http://www.codeproject.com/KB/aspnet/iis-aspnet-url-rewriting.aspx

Alex Ilyin
A: 

I have a sample application on IIS6.

I found quick-and-dirty solution. (Dirty, because it contains default view name with extension) It does not require additional route, or anything special. (Except, your default route must {controller}.aspx/{action}... format)

Here the default.aspx

<%@ Page Language="C#"%>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
    HttpContext.Current.RewritePath("~/Home.aspx/index");
    IHttpHandler httpHandler = new MvcHttpHandler();
    httpHandler.ProcessRequest(HttpContext.Current);
}
</script>

My sample applications default action was index, in Home directory.

Note : I saw this code at Phil Haack's blog. Thanks to Brian Lowe.

anov