HTML Error Returned on Production ASP.NET MVC 2 Site but Not on localhost
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>
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.
Probably you have configured Custom Errors pages on production server that are returned when errors ocurs. Try to turn it off in Web.config:
<customErrors mode="Off" />
精彩评论