简体   繁体   English

IIS URL重写模块:Url.Content()无法正确解析CSS / Image路径

[英]IIS URL Rewrite module: Url.Content() does not resolve CSS/Image path properly

I have a standard MVC3 project with layout page etc. Now I need to make pretty URLs. 我有一个标准的MVC3项目与布局页面等。现在我需要制作漂亮的URL。 I started playing with URL rewrite module. 我开始玩URL重写模块。 I'm trying to translate http://localhost/Photographer/Pablo into http://localhost/category-about.aspx?displayName=Pablo , and here is my rewrite rule (very simple!): 我正在尝试将http://localhost/Photographer/Pablohttp://localhost/category-about.aspx?displayName=Pablo ,这是我的重写规则(非常简单!):

  <system.webServer>
    <rewrite>
      <rules>
        <rule name="about" patternSyntax="Wildcard">
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_URI}" pattern="\.png|\.js|\.css|\.jpg" negate="true" />
          </conditions>
          <match url="photographer/*" />
          <action type="Rewrite" url="category-about.aspx?displayName={R:1}" logRewrittenUrl="true" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

all conditions you see I added after googling trying to solve the issue - they did not help though. 你看到我在google搜索试图解决问题后添加的所有条件 - 尽管他们没有帮助。

I found this page: http://www.iis.net/learn/extensions/url-rewrite-module/url-rewriting-for-aspnet-web-forms - which says that ~ operator is properly treated by the server when rewriting rules are applied. 我找到了这个页面: http//www.iis.net/learn/extensions/url-rewrite-module/url-rewriting-for-aspnet-web-forms - 这表示在重写时服务器正确处理〜运算符规则适用。 But that's clearly does not happen in my case - please see the image attached: 但在我的情况下显然不会发生这种情况 - 请参阅附图:

图像附加

What is the solution to my problem? 我的问题有什么解决方案? How should I reference CSS/JS files? 我该如何引用CSS / JS文件? I'm using MVC3 on IIS 7.5. 我在IIS 7.5上使用MVC3。

UPDATE: image is not very clear - but it shows that my MasterLayout page has 更新:图像不是很清楚 - 但它显示我的MasterLayout页面有

<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />

but it's resolved as 但它已经解决了

http://localhost/Photographer/Content/Site.css - and it gives 404

instead of 代替

http://localhost/Content/Site.css - which gives 200

when I request this URL: http://localhost/Photographer/Pablo . 当我请求此URL时: http://localhost/Photographer/Pablo Logic works fine - my controller gets the request and renders the page - but it's CSS and images are missing (because they have wrong root folder prepended). 逻辑工作正常 - 我的控制器获取请求并呈现页面 - 但它的CSS和图像丢失(因为它们有错误的根文件夹前置)。

Try using Request.ApplicationPath. 尝试使用Request.ApplicationPath。 Something like this should work: 这样的事情应该有效:

<link href="@(Request.ApplicationPath + "Content/Site.css")" rel="stylesheet" type="text/css" />

You said the line: 你说线:

<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" />

is resolved as 被解决为

"http:// localhost/Photographer/Content/Site.css" “http:// localhost / Photographer / Content / Site.css”

, which is absolutely correct this is how it would be resolved. ,这是绝对正确的,这是如何解决。 Where does your css lie, is the path for the image in css correct? 你的css在哪里,css中图像的路径是正确的吗?

Rather than this 而不是这个

<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />

Try this without the tilde (~) 试试这个没有波浪号(〜)

<link href="@Url.Content("/Content/Site.css")" rel="stylesheet" type="text/css" />

This should resolve to your desired http://localhost/Content/Site.css 这应该解析为您想要的http://localhost/Content/Site.css

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM