【发布时间】:2013-07-26 17:16:12
【问题描述】:
我在创建 api 控制器时出错因为我没有为 webapi 设置 autofac。
但是,我似乎无法在任何地方捕捉到异常。
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Type 'MyWeb.Web.Controllers.MyController' does not have a default constructor
</ExceptionMessage>
<ExceptionType>System.ArgumentException</ExceptionType>
<StackTrace>
at System.Linq.Expressions.Expression.New(Type type) at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
</StackTrace>
</Error>
我尝试为 Elmah 添加 WebApi contrib,然后添加了这个:
config.Filters.Add(new Elmah.Contrib.WebApi.ElmahHandleErrorApiAttribute());
没有让 elmah 注册异常。 我在 global.asax 中添加了以下内容:
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
Elmah.ErrorSignal.FromCurrentContext().Raise(exception);
}
没有任何区别。
如何处理在调用控制器之前发生的错误?
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-4 error-handling asp.net-web-api