Is it possible to run an MVC application from a virtual directory in IIS7? I have built an open source utility app in ASP.NET MVC3 and wondering if that was a mistake; it likely is if the site cannot be run from a virtual directory.
Take a si开发者_如何转开发mple default route of /home/index if running from a virtual directory named /app, will actually be /app/home index. Which kind of messes things up for routing.
I don't want a user to have to change routes and recompile the project to use the app in a virtual directory. Is there a way to change a configuration parameter to indicate what the root folder of what the application is?
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 /
Yes this works. And as long as you're using the helper methods to create action URLs (e.g. <%=Html.ActionLink(...) %>
there is no need to reconfigure or recompile.
Just in case it helps someone, I ran into an issue where my MVC actions are dynamic so I could not have .net generate the correct url's for me using the methods described in the accepted answer's post. I could use "~/" server side but not in javascript, etc. My solution was to write a shared method that builds the url using the application's name that I get from:
System.Web.HttpRuntime.AppDomainAppVirtualPath
Example: "/maint/Manage/Users" where "maint" is the name of my application that may be different on some servers, "Manage" is the name of my controller and "Users" is a dynamic action that comes out of a database:
return string.Format("{0}/Manage/{1}", HttpRuntime.AppDomainAppVirtualPath, MenuName);
精彩评论