Kiwihosting Support Forum: Developing MVC Applications - Kiwihosting Support Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Developing MVC Applications Using ISAPI_Rewrite

#1 User is offline   Kiwihost 

  • Group: Admin
  • Posts: 2361
  • Joined: 27-October 02

Posted 09 July 2009 - 07:52 PM

How to deploy ASP.NET 3.5SP1 MVC Apps using ISAPI_Rewrite

For the .htaccess File
RewriteEngine on

RewriteRule (.*)\.(css|gif|png|jpeg|jpg|js|zip) $1.$2 [I,L]
RewriteRule ^Home(/)?$ $9 [NC,R=301]
RewriteRule ^$ Home [NC]
RewriteRule ^([\w]+)$ $1.aspx [NC]
RewriteRule ^(?!Content)([\w]*)/(.*) $1.aspx/$2 [NC]


For your global.asax.cs File
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute("default",
"{controller}/{action}/{id}",
new { controller = "home", action = "index", id = "" },
new { controller = @"[^\.]*" } );

routes.MapRoute("default.aspx",
"{controller}.aspx/{action}/{id}",
new { controller = "home", action = "index", id = "" },
new { controller = @"[^\.]*" } );

// For non wildcard default requests (.aspx)
routes.MapRoute("root", "", new { controller = "home", action =
"index", id = "" } );
}


Some additional information can be found here http://blog.codevill...t-mvc-to-iis-6/ specifically Option 4 is what you need to use.
Kiwihosting.net Ltd
NZ Web Hosting Specialists
http://www.kiwihosting.net.nz

#2 User is offline   snaayk 

  • Group: Customers
  • Posts: 116
  • Joined: 14-February 04

Posted 07 April 2010 - 09:21 AM

I thought I'd share that this works with MVC 2, if you bin deploy it (until it's installed on the servers).

#3 User is offline   snaayk 

  • Group: Customers
  • Posts: 116
  • Joined: 14-February 04

Posted 07 April 2010 - 10:41 AM

I noticed that in the example you posted here, every controller will require a matching entry with the extension. This comes from the original post (inside the link above). However, the link above, does a rewrite in the htaccess with "rewrite.aspx" and then removes it during begin_request. This second version seems much simpler since there is no need for superflous entries in the route table. Additionally, you do not require the third parameter for each route entry.

Updated Rule:
RewriteRule ^(?!Content)(.*) /rewritten.aspx/$1 [NC]


Instead of
RewriteRule ^([\w]+)$ $1.aspx [NC]
RewriteRule ^(?!Content)([\w]*)/(.*) $1.aspx/$2 [NC]


Of course, you have to add the following code to Application_BeginRequest
protected void Application_BeginRequest(Object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            if (app != null)
                if (app.Request.AppRelativeCurrentExecutionFilePath == "~/rewritten.aspx")
                    app.Context.RewritePath(
                        app.Request.Url.PathAndQuery.Replace("/rewritten.aspx", "")
                    );
        }


And then the RegisterRoutes looks like:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute("Default", 
                "{controller}/{action}/{id}", 
                new { controller = "home", action = "index", id = "" });

routes.MapRoute("root", 
                "", 
                new { controller = "home", action ="index", id = "" } );



Is there a reason why you chose to use the initial example?

#4 User is offline   Kiwihost 

  • Group: Admin
  • Posts: 2361
  • Joined: 27-October 02

Posted 07 April 2010 - 01:37 PM

The initial example was provided by another customer. Thanks for your feedback though, this gives customers another option.

Cheers.
Kiwihosting.net Ltd
NZ Web Hosting Specialists
http://www.kiwihosting.net.nz

#5 User is offline   Kiwihost 

  • Group: Admin
  • Posts: 2361
  • Joined: 27-October 02

Posted 07 April 2010 - 01:48 PM

Also thanks for the heads up about the MVC 2 update, I'll schedule this to be installed on our next Server Patch/Reboot day which will be early next month.
Kiwihosting.net Ltd
NZ Web Hosting Specialists
http://www.kiwihosting.net.nz

#6 User is offline   snaayk 

  • Group: Customers
  • Posts: 116
  • Joined: 14-February 04

Posted 09 April 2010 - 10:17 AM

If you install the .NET 4 update (comes out April 12th, I beleive) it will include the MVC2 update. That'll be nice, sooo nice.

#7 User is offline   Kiwihost 

  • Group: Admin
  • Posts: 2361
  • Joined: 27-October 02

Posted 09 April 2010 - 04:26 PM

I'll put it on the to-do list :)

ED: .NET4 was installed last month as part of the patch cycle
Kiwihosting.net Ltd
NZ Web Hosting Specialists
http://www.kiwihosting.net.nz

#8 User is offline   snaayk 

  • Group: Customers
  • Posts: 116
  • Joined: 14-February 04

Posted 06 May 2010 - 10:35 AM

With ASP.NET 4 and MVC 2 (haven't tried with 2.0/3.5 or MVC 1) no rewrite rules are needed. It works as expected.

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users