【问题标题】:Url rewrite and custom error messageUrl 重写和自定义错误消息
【发布时间】:2015-10-15 08:35:25
【问题描述】:

我在主机上配置了一个网站和一个 api,监听 80 和 443。我正在尝试拆分 2 并将它们放在单独的端口上,并使用 IIS url 重写来路由请求。

所以,我创建了一个只有 web.config 的新空白站点并将我的重写规则放在那里,我的请求被正确路由,但是我无法让 API 返回 400 的情况正常工作。

即如果您直接使用 Bad 请求 ping API(指定端口),它会正确返回 400 Bad request json 响应

400 Bad Request

Pragma: no-cache
X-CorrelationId: 823fc2fd-4ed2-46b6-86e0-50b2abf5d61b
X-Content-Type-Options: nosniff
Request-Processing-Time: 606.4377 ms
OData-Version: 4.0
Content-Length: 142
Cache-Control: no-cache
Content-Type: application/json; odata.metadata=minimal; odata.streaming=true
Date: Wed, 14 Oct 2015 21:39:58 GMT
Expires: -1
P3P: CP="CAO DSP COR ADMa DEV CONi TELi CUR PSA PSD TAI IVDi OUR SAMi BUS DEM NAV STA UNI COM INT PHY ONL FIN PUR"
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET

{
  "error":{
    "code":"20112","message":"Invalid Entity ID specified.",
  }
}

但是通过 URL 重写你会看到一个 html 页面

400 Bad Request


Content-Length: 3506
Cache-Control: private
Content-Type: text/html; charset=utf-8
Date: Wed, 14 Oct 2015 21:33:42 GMT
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET

<!DOCTYPE html>
<html>
    <head>
        <title>A potentially dangerous Request.Path value was detected from the client (:).</title>
        <meta name="viewport" content="width=device-width" />
        <style>
         body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}
         p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
         b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
         H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
         H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
         pre {font-family:"Consolas","Lucida Console",Monospace;font-size:11pt;margin:0;padding:0.5em;line-height:14pt}
         .marker {font-weight: bold; color: black;text-decoration: none;}

我尝试将 &lt;httpErrors existingResponse="PassThrough"/&gt; 添加到 web.config 中,但没有帮助...

编辑:我有一个线索,它似乎与无效字符有关,api 有一个带有 : 的参数。还在想办法解决这个问题

编辑 2:在下面的答案中查看解决方案。

关于如何解决这个问题的任何提示?

【问题讨论】:

  • 您的请求看起来如何?

标签: asp.net url-rewriting iis-7.5


【解决方案1】:

所以,问题在于 api 允许在 url 中使用一些特殊字符,我必须在 web.config 中指定这些字符

查看链接:http://forums.iis.net/t/1193674.aspx

我最终在 web.config 中添加了以下行:

<httpRuntime targetFramework="4.5" requestPathInvalidCharacters="&lt;,&gt;,*,%,&amp;,\"/>

【讨论】:

    猜你喜欢
    • 2012-04-26
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    • 2015-09-21
    • 2011-04-27
    • 2018-09-02
    相关资源
    最近更新 更多