IIS URL重写模块:Url.Content()无法正确解析CSS/图像路径 [英] IIS URL Rewrite module: Url.Content() does not resolve CSS/Image path properly

查看:409
本文介绍了IIS URL重写模块:Url.Content()无法正确解析CSS/图像路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有布局页面等的标准MVC3项目.现在,我需要创建漂亮的URL.我开始使用URL重写模块.我正在尝试将http://localhost/Photographer/Pablo转换为http://localhost/category-about.aspx?displayName=Pablo,这是我的重写规则(非常简单!):

I have a standard MVC3 project with layout page etc. Now I need to make pretty URLs. I started playing with URL rewrite module. I'm trying to translate http://localhost/Photographer/Pablointo http://localhost/category-about.aspx?displayName=Pablo, and here is my rewrite rule (very simple!):

  <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.

我找到了此页面:

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. But that's clearly does not happen in my case - please see the image attached:

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

What is the solution to my problem? How should I reference CSS/JS files? I'm using MVC3 on IIS 7.5.

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

UPDATE: image is not very clear - but it shows that my MasterLayout page has

<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和图像丢失了(因为它们以错误的根文件夹开头).

when I request this 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).

推荐答案

尝试使用Request.ApplicationPath.这样的事情应该起作用:

Try using Request.ApplicationPath. Something like this should work:

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

这篇关于IIS URL重写模块:Url.Content()无法正确解析CSS/图像路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆