Url.Content在安装UrlRewrite模块时抛出异常,并在传递的参数中使用〜(代字号)

时间:2016-04-03 05:46:10

标签: .net asp.net-mvc iis url-rewrite-module

问题是:

  1. 我在IIS上为“A”网站
  2. 安装了UrlRewrite modole
  3. 我有“B”网站(MVC 5.0),它不使用“UrlRewrite”模块,但在调用UrlHelper.Content("~/blobl/foo/bar")时会出现异常。 例外是因为使用UrlRewriter模块并同时使用~字符作为函数参数。 例外是:

    System.ArgumentException:值不在预期范围内。 在System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode,IntPtr errorInfo) 在System.Web.Hosting.IIS7WorkerRequest.GetServerVariableInternal(String name) 在System.Web.WebPages.UrlRewriterHelper.WasThisRequestRewritten(HttpContextBase httpContext) 在System.Web.WebPages.UrlUtil.GenerateClientUrlInternal(HttpContextBase httpContext,String contentPath) 在System.Web.WebPages.UrlUtil.GenerateClientUrlInternal(HttpContextBase httpContext,String contentPath) 在System.Web.WebPages.UrlUtil.GenerateClientUrl(HttpContextBase httpContext,String contentPath)

  4. 如果我卸载UrlRewrite模块,一切都会正常,但“A”网站遇到问题。另外,我找不到任何方法只为网站禁用此模块。

    注意:我已经检查了IIS URL Rewrite module: Url.Content() does not resolve CSS/Image path properly,但我不想更改源代码。

2 个答案:

答案 0 :(得分:0)

我建议您添加 B 网站的网址,以忽略网址重写列表。

在web.config中添加以下内容:

<rule name="Ignore URLs" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAll">
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
        <add input="{REQUEST_URI}" negate="true" pattern="^/blobl/foo/bar\.aspx$" ignoreCase="true" />
        <add input="{REQUEST_URI}" negate="true" pattern="^/blobl/foo/bar/gar\.aspx$" ignoreCase="true" />
    </conditions>
</rule>

答案 1 :(得分:0)

经过长时间的搜索,我发现没有常规方法可以为网站启用UrlRewrite模块,而不会影响其他网站。
最简单的方法是: 通过web.config文件中的以下行从受影响的网站中删除此模块:

<configuration>
 <system.webServer>
   <modules>
      <remove name="RewriteModule" />
   </modules>
 </system.webServer>
</configuration>

有关详情,请查看:https://www.iis.net/configreference/system.webserver/modules