Tuesday, November 15, 2011

MVC / MVC?


Using a shared hosting server has a huge advantage of cost. Especially if you don’t expect large traffic. It does provide some unique challenges when dealing with configuration though.
By default, web.config files inherit from each other. Awesome news if your building sub folders to contain micro-sites of your original. You immediately get access to all your connections, provides, and even custom classes you previously imported. But when creating sub folders within your root directory housing a different domain, things get a little messy.
After getting a base MVC site set up locally I deployed it to my shared hosting solution. The first thing I noticed was the url routing was off. Instead of the typical “domain.com/{controller}/{action}” I was seeing “domain.com/{subfolder}/{controller}/{action}” While I could manually remove the subfolder from the url and still get a successful request. This was not ideal and I don’t think the end user is interested in my subfolder naming convention.
I knew I was going to have to use some url rewriter to accomplish this. It didn’t take long for me to find out my hosting provider has dealt with this before and has pre-installed a URL Rewrite Module on all their windows shared hosting accounts with IIS 7. Lucky me. Easy fix, all I had to do was add some specific xml to my subfolder web.config system.webServer section.
<rewrite>
     <rules>
          <rule name="Remove Virtual Directory">
               <match url=".*" />
               <action type="Rewrite" url="{R:0}" />
          </rule>
     </rules>
</rewrite>
The second piece was not inheriting the connections and providers from the parent web.config. There are two ways of doing this, the easiest is putting
<clear />
after your section deceleration and before your names. This worked for most of it, what the previous doesn’t cover is any attributes you may have declared in the section deceleration, such as
<membership defaultProvider="customProvider">
The clear needs to go after this line, but doesn’t clear the attributes. So how do we remove this from our config? First, hopefully you have access to the root directory. Now edit the root web.config and place a section before your <system.web>, it should look like this.
<location path="." inheritInChildApplications="false">
        <system.web>
            ....
        </system.web>
    </location>
The only thing you really need to be careful here is this will wipe out your entire <system.web> for any sub folder you create. So make sure your children applications have valid web.config’s. Enjoy!

No comments:

Post a Comment