Running an ASP.NET MVC app from a virtual directory in IIS7

Is it possible to run an MVC application from a virtual directory in IIS7?

Not only that it is possible but it is the preferred way.

Which kind of messes things up for routing.

Not if you use Html helpers when dealing with urls which will take care of this.

Here's a typical example of what you should never do:

<script type="text/javascript">
    $.ajax({
        url: '/home/index'
    });
</script>

and here's how this should be done:

<script type="text/javascript">
    $.ajax({
        url: '@Url.Action("index", "home")'
    });
</script>

Here's another typical example of something that you should never do:

<a href="/home/index">Foo</a>

and here's how this should be written:

@Html.ActionLink("Foo", "Index", "Home")

Here's another example of something that you should never do:

<form action="/home/index" method="opst">

</form>

and here's how this should be written:

@using (Html.BeginForm("Index", "Home"))
{

}

I think you get the point.


Yes, that works fine, and no, it doesn't mess up routing. However, the app you're running may be buggy and not support that configuration.

You don't need a "configuration parameter," because IIS and ASP.NET already handle this correctly.

You do, however, need to avoid hard-coded URIs in your views.

E.g., do this:

<img src="<%: Url.Content("~/Content/Images/Image.png") %>" />

...instead of:

<img src="/Content/Images/Image.png" />

...and similarly for links and style sheet references.


as far as i know routes are all based on the application root, not the actual root, so think of them as beginning with ~/, not /