3

我有一个带有布局页面等的标准 MVC3 项目。现在我需要制作漂亮的 URL。我开始玩 URL 重写模块。我正在尝试翻译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>

您在谷歌搜索尝试解决问题后添加的所有条件 - 但它们没有帮助。

我找到了这个页面:http ://www.iis.net/learn/extensions/url-rewrite-module/url-rewriting-for-aspnet-web-forms - 这表示〜运算符在重写时被服务器正确处理应用规则。但这显然不会在我的情况下发生 - 请参阅所附图片:

附图片

我的问题的解决方案是什么?我应该如何引用 CSS/JS 文件?我在 IIS 7.5 上使用 MVC3。

更新: 图像不是很清楚 - 但它表明我的 MasterLayout 页面有

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

但它被解决为

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

代替

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

当我请求此 URL 时:http://localhost/Photographer/Pablo。逻辑工作正常 - 我的控制器获取请求并呈现页面 - 但它的 CSS 和图像丢失(因为它们前面有错误的根文件夹)。

4

3 回答 3

4

尝试使用 Request.ApplicationPath。像这样的东西应该工作:

<link href="@(Request.ApplicationPath + "Content/Site.css")" rel="stylesheet" type="text/css" />
于 2013-09-05T06:37:27.637 回答
2

你说这行:

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

被解决为

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

,这是绝对正确的,这就是解决方法。你的css在哪里,css中图像的路径是否正确?

于 2013-09-04T12:54:30.163 回答
0

而不是这个

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

试试这个不带波浪号 (~)

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

这应该解决您想要的http://localhost/Content/Site.css

于 2013-09-05T17:42:46.727 回答