Running an ASP.NET MVC Application from a Virtual Directory in IIS7

Can I run an MVC application from a virtual directory in IIS7? I created an open source application in ASP.NET MVC3 and am wondering if this was an error; most likely if the site cannot be started from the virtual directory.

Take the simple default route / home / index , if it is run from a virtual directory with the name / app , it will actually be / app / home index. What kind of things are useless for routing.

I do not want the user to change routes and recompile the project in order to use the application in a virtual directory. Is there a way to change the configuration parameter to indicate which root folder for the application?

+6
source share
4 answers

Can I run an MVC application from a virtual directory in IIS7?

Not only is this possible, but it is the preferred method.

What type of mess is for routing.

Not if you use HTML helpers when dealing with URLs that take care of this.

Here is a typical example of what you should never do:

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

and here is how to do it:

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

Here is another typical example of what you should never do:

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

and this is how it should be written:

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

Here is another example of what you should never do:

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

and this is how it should be written:

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

I think you get the point.

+19
source

Yes it works. And while you are using helper methods to create action URLs (e.g. <%=Html.ActionLink(...) %> , there is no need to reconfigure or recompile.

+2
source

Yes, it works great, and no, it won't ruin the routing . However, the application that you run may be erroneous and does not support this configuration.

You do not need a “configuration parameter” because IIS and ASP.NET already handle this correctly.

However, you should avoid encoded URIs in your views.

For example, do the following:

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

... instead of:

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

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

+2
source

as far as I know, all routes are based on the root of the application, and not on the root itself, so think about them starting with ~/ and not /

+2
source

Source: https://habr.com/ru/post/890611/


All Articles