在asp.net mvc应用程序中,我成功处理了从JavaScript到Controller进行jquery $ .ajax调用时发生的异常。当我从javascript进行javascript fetch()调用时,我想以相同的方式处理错误,但是我不知道如何识别进行了fetch()调用。
我正在使用从HandleErrorAttribute派生的类来处理所有Controller错误。这是通过将以下行添加到global.asax Application_Start来完成的:
GlobalFilters.Filters.Add(new Areas.Insurance.Helpers.MyHandleErrorAttribute());
在MyHandleErrorAttribute中,我重写了OnException方法,以便与常规get或post错误区别对待Ajax错误。如果出现ajax错误,我希望返回一个包含guid的对象并触发javascript中的$ .ajax“ error:”回调。如果有由javascript fetch()调用引起的错误,我希望response.ok返回“ false”并传递回包含错误guid的对象。如果错误不是ajax错误或javascript fetch()错误,我想重定向到我们的自定义错误页面。代码如下:
public override void OnException(ExceptionContext filterContext)
{
if (filterContext.Exception == null)
return;
//log the error
Guid guid = MyClass.LogError(filterContext.Exception.GetBaseException());
if (filterContext.HttpContext.Request.IsAjaxRequest()
&& filterContext.Exception != null)
{
//this is an error from a $ajax call.
//500 is needed so that $ajax "error:" function is hit, instead of "success:"
filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
filterContext.Result = new JsonResult()
{
Data = new MyErrorObject()
{
Guid = guid
}
,JsonRequestBehavior = JsonRequestBehavior.AllowGet
//JsonRequestBehavior.AllowGet prevents following sporadic error: System.InvalidOperationException: This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.
};
//this stops Application_OnError from firing
filterContext.ExceptionHandled = true;
//this stops the web site from using the default IIS 500 error.
//in addition must set existingResponse="Auto" ("PassThrough also works) in web.config httpErrors element.
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
}
else
{
//if condition to avoid error "Server cannot set status after HTTP headers have been sent"
if (!filterContext.HttpContext.Response.IsRequestBeingRedirected)
{
filterContext.Result = new RedirectToRouteResult(
new System.Web.Routing.RouteValueDictionary
{
{"action", "Error500" },
{"controller", "Error" },
{ "Guid", guid}
});
filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.OK;
filterContext.ExceptionHandled = true;
}
}
}
我的问题是,使用javascript fetch()进行调用时,“ IsAjaxRequest”返回false,因此未命中第一个“ if”块。相反,我进入“ else”块,该块将Http响应设置为“ OK”,并重定向到我的错误页面,这都不是必需的(当我遇到fetch()错误时,我想返回500响应,当然重定向无效。
如何识别进行了javascript fetch()调用?是否有类似于“ IsAjaxRequest”的内容?
这是javascript提取的样子:
let url = '/myDomain/ThrowException';
fetch(url)
.then(function (data) {
if(data.ok){
//do something with data
}
else{
handleHttpErrorFunction(data);
}
})
.catch(function (error) {
handleFetchErrorFunction(error);
});
这是我要替换为fetch()调用的(成功处理的)jquery ajax调用:
$.ajax({
url: '/myDomain/ThrowException',
type: 'GET',
success: function (data) {
//do something with data
},
error: function (XMLHttpRequest, ajaxOptions, ex) {
ajaxerrorhandler(XMLHttpRequest);
}
答案 0 :(得分:0)
请参阅From server, how recognize javascript fetch() call was made (asp.net mvc)上相关问题的答案。解决方法是在标头中发送X-Requested-With XMLHttpRequest:
fetch('/MyDomain/MyControllerAction', { headers: { 'X-Requested-With': 'XMLHttpRequest' } })