你可以做財產以後這樣的屬性
[AttributeUsageAttribute(AttributeTargets.All, Inherited = true, AllowMultiple = true)]
public class ExceptionActionFilter : ExceptionFilterAttribute
{
private static Logger _log ;//= LogManager.GetLogger("mysite");
public override void OnException(HttpActionExecutedContext contex)
{
if (_log == null)
_log = LogManager.GetCurrentClassLogger();
var ex = contex.Exception;
_log.Error(ex);
contex.Response = contex.Request.CreateResponse(HttpStatusCode.OK,
new
{
ErrorMessage = contex.Exception.Message,
RealStatusCode = (int)(ex is NotImplementedException || ex is ArgumentNullException ? HttpStatusCode.NoContent : HttpStatusCode.BadRequest),
ReturnUrl = CommonContext.ErrorUrl
},
new JsonMediaTypeFormatter());
base.OnException(contex);
}
}
然後把屬性的一類EXC並在CLIEN側
或過濾
public class ExceptionLoggerFilter : System.Web.Http.Filters.IExceptionFilter
{
private static Logger _log;
public ExceptionLoggerFilter()
{
if (_log == null)
_log = LogManager.GetCurrentClassLogger();
}
public bool AllowMultiple { get { return true; } }
public System.Threading.Tasks.Task ExecuteExceptionFilterAsync(
System.Web.Http.Filters.HttpActionExecutedContext contex,
System.Threading.CancellationToken cancellationToken)
{
return System.Threading.Tasks.Task.Factory.StartNew(() =>
{
_log.Error(contex.Exception);
contex.Response = contex.Request.CreateResponse(HttpStatusCode.OK,
new { RealStatusCode = (int)HttpStatusCode.Forbidden, ReturnUrl = "#/error.html"},
contex.ActionContext.ControllerContext.Configuration.Formatters.JsonFormatter);
}, cancellationToken);
}
}
然後在全球.asax.cs
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
Database.SetInitializer<MySiteContext>(null);
}
和
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)//RouteCollection config)//
{
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
config.Filters.Add(new ExceptionLoggerFilter());
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional });
}
}
而在客戶端梅比財產以後像
$.controller.ajax.promise = controller.ajax.promise = function (obj)
{
var deferred = q.defer();
$.ajax({
type: obj.type || "GET",
url: obj.url,
context: obj.context || null,
data: obj.data || null,
contentType: obj.contentType || "application/json; charset=utf-8",
dataType: obj.dataType || "json",
success: function (res, textStatus, jqXHR)
{
if (res.RealStatusCode)
{
switch (res.RealStatusCode)
{
case 400://x error
res.ClientMessage = res.ErrorMessage;
deferred.reject(res);
break;
case 408://y errors
location.href = res.ReturnUrl;
return false;
case 403://ext
msgbox.alert({
message: 'Ma belle msg',
title: "Error"
});
deferred.reject();
location.href = res.ReturnUrl;
return false;
default:
deferred.reject();
location.href = res.ReturnUrl;
break;
}
}
deferred.resolve(res);
return true;
},
error: function (jqXHR, textStatus, errorThrown)
{
deferred.reject({ msg: jqXHR.statusText, jqXHR: jqXHR, textStatus:textStatus, errorThrown:errorThrown });
}
});
return deferred.promise;
};
我希望這可以幫助其他的Google那裏! (就像@Thomas CG de Vilhena說的那樣=)
兩年後,我用google搜索了一下,結果在這裏,看到我的名字xD –
yess [taht ans](https://stackoverflow.com/a/19480499/1509853 )幫助,再次10倍=) – oCcSking