Styles and Scripts not Loading After URL-Rewriting

心已入冬 提交于 2019-12-11 09:02:59

问题


I have applied a URL-rewrite rule to my web-site using UrlRewritingNet.UrlRewriter.dll extension. On the web.config file I have written rules like the following:

<add name="Default"
     virtualUrl="^~/(.*)/Default.zkn"
     rewriteUrlParameter="ExcludeFromClientQueryString"
     destinationUrl="~/Default.aspx?lang=$1"
     ignoreCase="true"
/>

It easily changes http://sitename.com/en/Default.zkn to Default.aspx?lang=en

A part of my head section is like the following:

<link href="Styles/Default.css" type="text/css" rel="Stylesheet" media="screen" />

But since I have applied this, The styles and the additional javascript files pointed relatively on the head section are not loaded.

Then I have tried to add ~ character.

<link href="~/Styles/Default.css" type="text/css" rel="Stylesheet" media="screen" />

Many people said this would work. But no, it did not work.

After page loads I open the source on the browser and I see that ~ character was not replaced with my site folder.

So a 404 error occurs for the css and js files.

How can it be corrected?

Is there a way to add a new URL-rewriting rule for the style/js files also?


回答1:


Maybe you can try :

<%
var basePath = String.Format("{0}{1}",Request.ApplicationPath,  string.IsNullOrEmpty(Request.ApplicationPath) || !Request.ApplicationPath.EndsWith("/") ? "/" : "");
%>

<link href="<%=basePath%>Styles/Default.css" type="text/css" rel="Stylesheet" media="screen" />


来源:https://stackoverflow.com/questions/16208473/styles-and-scripts-not-loading-after-url-rewriting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!