Is it possible to run an MVC application in IIS7 from a virtual directory? I built an open-source utility app in ASP.NET MVC3 and am wondering if this was a mistake, it is most likely if the site cannot be run from a virtual directory.
Consider the simple default route /home/index. It runs from a virtual directory called /app, the path will be /app/home index. That kind of messes up the routing.
To use the app in a virtual directory, I don't want the user to have to change routes and recompile the project. Is it possible to change a configuration parameter to indicate the application's root directory?
Not only is it possible, but it is also the preferred method.
Not if you use HTML helpers when dealing with URLs, which will handle this for you.
Here's an example of something you should never do:
< script type="text/javascript"> $.ajax({ url: '/home/index' }); < /script>
and here's how this should be execute:
< script type="text/javascript"> $.ajax({ url: '@Url.Action("index", "home")' }); < /script>
Here's another example of something that you should never do:
< a href="/home/index">Foo< /a>
and here's how this should be entered:
@Html.ActionLink("Foo", "Index", "Home")
Here's another example of something that you should not do:
< form action="/home/index" method="post"> < /form> and here's how this should be written: @using (Html.BeginForm("Index", "Home")) { }
I think you get what it means.
I remember having an identical problem and I counted this line to the code.
protected void Application_Error() { var exc = Server.GetLastError(); var httpExc = exc as HttpException; Response.Clear(); Server.ClearError(); var routeData = new RouteData(); routeData.Values["controller"] = "Error"; routeData.Values["action"] = "General"; routeData.Values["exception"] = exc; Response.StatusCode = 500; if (httpExc != null) { Response.StatusCode = httpExc.GetHttpCode(); routeData.Values["action"] = "Http404"; routeData.Values["exception"] = httpExc; } Response.TrySkipIisCustomErrors = true; //this fixed it IController errorsController = new WWS_Website.Controllers.ErrorController(); var rc = new RequestContext(new HttpContextWrapper(Context), routeData); errorsController.Execute(rc); }
From doing research, I discovered that the only way to create this blog is to create a 2nd "admin" site that operates as an application under the main site. Doing so, permitted me to set up approvals on that site.
I would utilize the UrlHelper to generate the links. This would guarantee that they are comparable to the application path.
< a href="<%= Url.Action( "action", "controller" ) % >">Link Text< /a>and
< img src="<%= Url.Content( "~/images/myimg.jpg" ) %>" alt="My Image" />
If you have any doubt about IIS7 is running an ASP.NET MVC app from a virtual directory. Don’t hesitate to contact us. Airo Global Software will be your digital partner.
E-mail id: [email protected]
Author - Johnson Augustine
Chief Technical Director and Programmer
Founder: Airo Global Software Inc
LinkedIn Profile: www.linkedin.com/in/johnsontaugustine/