代码之家  ›  专栏  ›  技术社区  ›  user1829319

部署Azure网站时的奇怪行为-ASP。net MVC和自定义错误过滤器

  •  0
  • user1829319  · 技术社区  · 10 年前

    我在部署ASP时遇到了一个非常奇怪的行为。Net网站到Azure。部署到Azure时,自定义筛选器的行为不同。问题是在自定义异常过滤器中生成的JsonResult没有在Azure中序列化JSON对象或其他内容。

    这是中的浏览器控制台中打印的自定义筛选器的结果 发展 :

    对象{data:对象,状态:400,配置:对象,状态文本:“错误请求”} 我可以看到“数据”是一个对象,具有以下属性: 数据:对象 details:“堆栈详细信息…” 消息:“需要网站!”

    在Azure中,同样的事情是:

    对象{数据:“错误请求”,状态:400,配置:对象,状态文本:“错误的请求”} config:对象 data:“错误请求”

    我可以看到数据只是字符串!

    我的自定义过滤器如下:

    public class CustomErrorFilter : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
            //No further processing when the exception is handled 
            if (filterContext.ExceptionHandled)
            {
                return;
            }
    
            if (!(filterContext.Exception is BusinessException)) // Non business exceptions
            {
                //Logging the actual exception
                ElmahLogger.LogError(filterContext.Exception);
                System.Diagnostics.Trace.TraceError(filterContext.Exception.Message);
    
                //Changing exception type to a generic exception
                filterContext.Exception = new SystemException("An error occurred and logged during processing of this application. We appologise for any in-convieneces.");
    
                HandleAjaxException(filterContext, HttpStatusCode.InternalServerError);
    
                filterContext.ExceptionHandled = true;
            }
            else
            {
                System.Diagnostics.Trace.TraceError(filterContext.Exception.Message);
    
                HandleAjaxException(filterContext, HttpStatusCode.BadRequest);
    
                filterContext.ExceptionHandled = true;
            }
    
        }
    
        private static void HandleAjaxException(ExceptionContext filterContext, HttpStatusCode httpStatusCode)
        {
    
            filterContext.HttpContext.Response.StatusCode = (int)httpStatusCode;
    
    **// Seems like this is not working in Azure somehow????**
            filterContext.Result = new JsonResult
            {
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                Data = new
                {
                    message = filterContext.Exception.Message,
                    details = filterContext.Exception.StackTrace
                }
            };
    
            System.Diagnostics.Trace.TraceError("HandleAjaxException" + filterContext.Exception.Message);
    
        }
    
    }
    

    有人能指出发生了什么事吗?

    1 回复  |  直到 10 年前
        1
  •  0
  •   user1829319    10 年前

    好吧,我终于想通了。需要更改web。默认情况下,配置文件IIS8不显示详细错误。这修复了它:添加 <httpErrors existingResponse="PassThrough"/> 内部的线 <system.webServer> .

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

    这将确保向浏览器呈现上游错误。

    推荐文章