在我的应用程序中,我有一个区域呼叫移动。在这个区域,我有一个文件夹调用Assets,我们放置所有的CSS,JavaScript和图像。
访问视图效果很好。 http://localhost/webapp/mobile/stuff
问题是当我访问我的css,javascript和图像时。
在我看来,我无法做到这一点。
<img src="Assets/css/styles.css" />
原因是真正的网址是:http://localhost/webapp/areas/mobile/assets/css/styles.css
有没有办法将http://localhost/webapp/areas/mobile映射到http://localhost/webapp/mobile以获取静态文件?
谢谢, 兰德尔
答案 0 :(得分:3)
我想出的解决方案是使用一个简单的IHTTPModule,它会为我重写网址。这是我为BeginRequest事件实现的方法。
void BeginRequest(object sender, EventArgs e)
{
var context = HttpContext.Current;
var path = context.Request.Path;
// Add Areas to the Mobile path, so we don't have to specify it manually.
// This is mainly for handling static resources that you place in areas.
if (!path.Contains("Mobile/")) return;
if (path.EndsWith(".png") ||
path.EndsWith(".gif") ||
path.EndsWith(".css") ||
path.EndsWith(".js") ||
path.EndsWith(".htm") ||
path.EndsWith(".cache"))
context.RewritePath(path.Replace("Mobile/", "Areas/Mobile/"));
}
答案 1 :(得分:1)
你可能需要这样做:
<img src="../../Mobile/Assets/css/styles.css" />
...取决于您当前目录的实际位置。您也可以尝试以下方式:
<img src="~Mobile/Assets/css/styles.css" />
找出实际路径应该是什么的好方法是将资源(在本例中为styles.css)从Project Explorer拖到视图中,然后让Visual Studio为您创建路径。