C# 在生产ASP.NET MVC 2站点上返回HTML错误,但在本地主机上未返回

C# 在生产ASP.NET MVC 2站点上返回HTML错误,但在本地主机上未返回,c#,xml,iis,mime-types,http-status-codes,C#,Xml,Iis,Mime Types,Http Status Codes,我使用ASP.NET MVC2创建了一个RESTful API,它以XML形式返回所有数据: string xml = Serialize(Data, context, AcceptCharsetList, encoding); context.HttpContext.Response.ContentEncoding = encoding; context.HttpContext.Response.Charset = encoding.WebName; context.HttpContext.R

我使用ASP.NET MVC2创建了一个RESTful API,它以XML形式返回所有数据:

string xml = Serialize(Data, context, AcceptCharsetList, encoding);
context.HttpContext.Response.ContentEncoding = encoding;
context.HttpContext.Response.Charset = encoding.WebName;
context.HttpContext.Response.ContentType = "application/xml";
context.HttpContext.Response.Write(xml);
在我的本地主机上,这对于正常响应(模型+视图)和错误(错误模型+错误视图+http状态代码)都可以正常工作

但是在实际的web服务器上,只有正常的请求返回xml。对于错误,它不起作用,并且错误作为html提供,内容类型为text/html

我的本地主机是带有IIS 7.5的64位windows 7,我的web服务器是带有IIS 7.5的windows 2008 64位

有什么不对劲吗

预期的XML如下所示:

<?xml version="1.0"?>
<Error xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
 xmlns="http://api.example.com/">
  <Description>(403) Forbidden.</Description>
</Error>

(403)禁止。
但它会在web服务器上返回此HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>403 - Forbidden: Access is denied.</title>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
 <div class="content-container"><fieldset>
  <h2>403 - Forbidden: Access is denied.</h2>
  <h3>You do not have permission to view this directory or page using the 
  credentials that you supplied.</h3>
 </fieldset></div>
</div>
</body>
</html>

403-禁止:访问被拒绝。
服务器错误
403-禁止:访问被拒绝。
您没有权限使用查看此目录或页面
您提供的凭据。

您可能已经在生产服务器上配置了当出现错误时返回的自定义错误页面。尝试在Web.config中将其关闭:


看来IIS正在返回自己的错误页面。在IIS管理器中,导航到您的应用程序,并在“功能”视图中,查看IIS部分(而不是ASP.NET部分)下的错误页面


这看起来像是在讨论同一个问题,作者使用该属性来处理它。我不确定这是否适用于您的情况-希望是这样。

猜得好,但我已经知道了,您可以阅读您的错误响应内容吗?它包含什么?我已经用预期的XML和要发送的HTML更新了问题。到目前为止看起来不错,很好。当你不知道名字的时候,很难用谷歌搜索那些模糊的设置!它现在正在为大多数请求返回XML。。但有时仍然返回html。正在尝试让它100%工作。@JK,我想知道您看到的html响应是否已缓存。无论如何,我很高兴你能成功。
<customErrors mode="Off" />