Can't find content when running on server with asp.net mvc 3. (Caused by routing)
I have an app setup that runs on a server where the url is rewritten to the app. it goes servername.com/myapp
I routed the app as follows:
routes.MapRoute(
"Default", // Route name
"myapp/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
So that it will always begin with "myapp" otherwise the server will redirect the url. Now the content can't be found. I've tried putting the urls in manually but it doesn't work. Here's what my view looks like:
<link href="@Url.Content("~/Content/themes/Site.css")" rel="stylesheet" type="text/css" />
Nothing strange开发者_如何学JAVA about this so I don't see why it shouldn't work. Perhaps the server is rewriting the urls for content as well, so is there any way to set Url.Content() to map accordingly?
I recently had the same problem. Here is what I used to solve it, although someone may find a somewhat easier way.
On the development webserver, it used the server's root which resolved as "/" and it resolved to "/appname" on the deployment webserver. I could hardcode to link to one, but it obviously failed for the other. The answer is to create strings that find it programmatically.
@{
string rootpath = HttpContext.Current.Request.ApplicationPath;
if(rootpath != "/") { rootpath = rootpath + "/"; }// add closing slash if missing
string contentpath = rootpath + "Content/themes/Site.css";
}
<link href="@contentpath" rel="stylesheet" type="text/css" />
I put the rootpath code into a static function in a separate class since I used it on every page with links or images.
精彩评论