问题
I have an MVC application that has been moved to a sub folder in the wwwroot, and this sub folder has been made an application via IIS. So whereas before the url to my login page would look like this:
www.mydomain.com/login
It now looks like this:
www.mydomain.com/application/login
The application works fine, it doesn't seem to have affected the routing or any of the links in my application. However, it is causing problems where I am referencing image src's like so:
<img src="/content/images/myimage.png"/>
As it is attempting to retrieve the image from the url www.mydomain.com/content/images/myimage.png which obviously returns a 404.
In my global.asax file I am only using the default route, which I have attempted to modify to account for the subfolder as part of the url:
routes.MapRoute("Default",
"application/{controller}/{action}/{id}",
new { controller = "dashboard", action = "index", id = UrlParameter.Optional });
However, now when I attempt to go to the root url www.mydomain.com/application I just get a directory listing! And if I try to go directly to controller i.e www.mydomain.com/application/dashboard I get a 404!
Anyone got any idea how to deal with this situation via MVC routing?
回答1:
- You don't need to change your routing. It works from entry point to your app.
- Use @Url.Content("~/content/images/myimage.png").
Hope this will help.
回答2:
Try this one...
routes.MapRoute("Application",
"application/{controller}/{action}/{id}",
new { controller = "dashboard", action = "index", id = UrlParameter.Optional });
routes.MapRoute("Default",
"{controller}/{action}/{id}",
new { controller = "dashboard", action = "index", id = UrlParameter.Optional });
来源:https://stackoverflow.com/questions/10330491/how-to-deal-with-iis-sub-applications-using-mvc-routing