Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 如何防止MVC4WebAPI返回错误请求的HTML_Asp.net Mvc 4_Asp.net Web Api - Fatal编程技术网

Asp.net mvc 4 如何防止MVC4WebAPI返回错误请求的HTML

Asp.net mvc 4 如何防止MVC4WebAPI返回错误请求的HTML,asp.net-mvc-4,asp.net-web-api,Asp.net Mvc 4,Asp.net Web Api,我已经使用默认项目模板(Visual Studio 2010,.NET 4.0)设置了一个全新的空白ASP.NET MVC 4 Web Api项目。当我浏览到http://localhost:5234/Api/Values/a..a,我得到以下HTML结果: <html> <head> <title>Bad Request</title> ... 错误的请求 ... 如何覆盖此行为,以便返回自定义XML

我已经使用默认项目模板(Visual Studio 2010,.NET 4.0)设置了一个全新的空白ASP.NET MVC 4 Web Api项目。当我浏览到
http://localhost:5234/Api/Values/a..a
,我得到以下HTML结果:

<html>
    <head>
        <title>Bad Request</title>
        ...

错误的请求
...

如何覆盖此行为,以便返回自定义XML或JSON错误结果?

尝试在Web.Config中设置此选项

  <system.webServer>
    <httpErrors existingResponse="PassThrough" />
  </system.webServer>


您的URL正确吗?你真的有这个/a..a/?@ThiagoCustodio我故意在这里创建一个错误的请求。我想要一个错误请求的自定义错误消息。避免
400错误请求的最佳方法是执行
Good request
@塞吉奥:你能保证我的API客户端总是会做一个好的请求吗?
:)@ZaidMasud这是一条路要走。您的客户需要知道他们的请求是否与Response和http headerTried不符,这是一个很好的猜测,但make不幸没有任何区别。