Url rewrite and custom error message

2019-09-11 11:06发布

问题:

I have a website and a api configured on my host listening on 80 and 443. I am trying to split the 2 and put them on separate ports and use IIS url rewrite to route requests.

So, I created a new blank site with just a web.config and put my rewrite rules there, my requests are being routed correctly, however I am not able to get the case working where the API returns 400.

i.e. if you ping the API with a Bad request directly (specify the port), it correctly returns a 400 Bad request json response

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.",
  }
}

However through URL rewrite you see a html page

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;}

I have tried adding <httpErrors existingResponse="PassThrough"/> to the web.config but that didn't help...

Edit: I have a lead, it seems to be related to invalid chars, the api has a param with : in it. Still trying to figure out how to tackle that

Edit 2: See resolution in the answer below.

Any tips on how to resolve this?

回答1:

So, the problem was that the api allows some special characters in the url, which I had to specify in the web.config

See link: http://forums.iis.net/t/1193674.aspx

I ended up adding the following line to the web.config:

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