我在global.asax中有以下代码,当有404异常时,它会转移到静态NotFound.aspx文件。这适用于我的开发机器,具有调试或发布版本。将发布版本部署到azure app服务时,我得到的页面只包含文本:The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
我已验证静态部署中存在静态文件。
global.asax中的代码是:
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
Response.Clear();
HttpException httpException = exception as HttpException;
if (httpException != null)
{
ErrorLogger.Log(httpException);
Server.ClearError();
switch (httpException.GetHttpCode())
{
case 404:
// page not found
Response.StatusCode = 404;
Server.Transfer("~/NotFound.aspx");
break;
default:
Response.StatusCode = 500;
Server.Transfer("~/Error.aspx");
break;
}
}
}
答案 0 :(得分:1)
问题似乎是Azure服务器环境中定义了它的httpErrors配置部分,以便在它们到达Application_Error之前拦截这些错误。你可以修改它以让错误通过,或者用它来处理错误(这似乎是最好的选择)。使用import serial
serialport = serial.Serial("/dev/ttyAMA0", 9600, timeout=10)
while True:
response = serialport.readline()
if not response: print "Haven't received input in 10s"
print response
serialport.close()
可以避免必须发出重定向,只需直接提供自定义错误页面和正确的状态代码。这似乎是一种更有效和正确的方法。
例如:
responseMode="File"
欲了解更多信息:
https://www.iis.net/configreference/system.webserver/httperrors
答案 1 :(得分:0)
您还可以尝试在Web.config
中指定重定向规则:
<configuration>
<system.webServer>
<httpErrors errorMode="DetailedLocalOnly" existingResponse="PassThrough">
<remove statusCode="404"/>
<add statusCode="404" path="/NotFound.aspx" responseMode="Redirect" />
</httpErrors>
</system.webServer>
</configuration>
然后在您的Web.Release.config
(或您在Azure中使用的其他配置)中:
<configuration>
<system.webServer>
<httpErrors errorMode="DetailedLocalOnly" existingResponse="Replace" xdt:Transform="SetAttributes">
</httpErrors>
</system.webServer>
</configuration>
您可以以类似的方式添加代码500错误页面。
将responseMode设置为Redirect使IIS使用302重定向用户,将其设置为ExecuteURL将使用错误页面替换响应,但将URL保留在地址栏中。
以下是关于以这种方式处理错误的好文章:http://tedgustaf.com/blog/2011/5/custom-404-and-error-pages-for-asp-net-and-static-files/