在生产 ASP.NET MVC 2 站点上返回 HTML 错误,但在本地主机上未返回
我有一个使用 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。对于错误,它不起作用,并且错误将作为内容类型 = text/html 的 html 提供。
我的本地主机是 64 位 Windows 7,带有 IIS 7.5,我的 Web 服务器是 Windows 2008 64 位,带有 IIS 7.5
可能出了什么问题?
预期的 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>
但它在 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>
I have a RESTful API created with ASP.NET MVC2 that returns all data as 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);
On my localhost that works fine for both normal responses (model + view) and for errors (error model + error view + http status code).
But on the actual web server only normal requests return xml. For errors it does not work, and the error is served as html with content type = text/html.
My localhost is 64 bit windows 7 with IIS 7.5 and my web server is windows 2008 64 bit with IIS 7.5
What could be wrong?
The expected XML is this:
<?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>
But it is returning this HTML instead on the web server:
<!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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来 IIS 正在返回它自己的错误页面。在 IIS 管理器中,导航到您的应用程序,然后在“功能”视图中,查看 IIS 部分(而不是 ASP.NET 部分)下的错误页面。
这个博文看起来是在讨论同样的问题,作者使用了HttpResponse.TrySkipIisCustomErrors 属性来处理它。我不确定这是否适用于您的情况 - 希望如此。
It looks like IIS is returning it's own error pages. In IIS Manager, navigate to your application, and in the Features view, look at the Error Pages under the IIS section (not the ASP.NET section).
This blog post looks like it is discussing the same problem, and the author uses the HttpResponse.TrySkipIisCustomErrors property to handle it. I'm not sure if this is applicable in your situation - hopefully it is.
您可能已在生产服务器上配置了在发生错误时返回的自定义错误页面。尝试在Web.config中将其关闭:
Probably you have configured Custom Errors pages on production server that are returned when errors ocurs. Try to turn it off in Web.config: