English 中文(简体)
例外处理 ASP.NET MVC Web AP
原标题:Exception Handling ASP.NET MVC Web API

我先说,是,我们已经并且正在使用一种例外的过滤器,从例外情况下继承。 在我国身份过滤器和工作之后,如果在安森软件的某个地方发生错误,则该仪器在一栏中登记。

尽管如此,我还是想找到一种办法,处理在提交APIC之前发生的错误。

<>Reasoning: 我们从不希望回去一个电离层和/或电离层电离层的超文本错误。 我们ALWAYS想要打上一种习俗例外过滤器/手压器,以便我们能够正确处理伐木问题,并向用户退回一名JSON的回复。

从现在起,利用菲德勒提出请求,我可以随头3wp.exe程序,看到申请受到打击。 Begin Request Approach in the global.asax. 之后,它只是回复了500份答复。 它从未例外地打断法典,或在之后打过任何我的休息点。 似乎正在退回一个局错误。 我们永远不想发生这种情况。 我们需要能够抓住所有这些“低级”例外,将其记录下来,并向用户归还有意义的东西。

难道我们能够做些什么来处理以前的错误,似乎正在打着伙伴关系。 NET MVC 网页预报密码?

最佳回答

Albeit I like Darin s answer it doesn t work in our case since the ASP.NET MVC Web API framework is suppressing/handling exceptions internally and not re-throwing to hit the Application_Error method in the Global.asax. Our solution is this.

我最后提出了一种习俗,即:

public class PrincipalHandler : DelegatingHandler
{
    protected const string PrincipalKey = "MS_UserPrincipal";
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        setAnonymousPrincipal();

        request = InitializeIdentity(request);

        return base.SendAsync(request, cancellationToken)
            .ContinueWith(r =>
                              {
                                  // inspect result for status code and handle accordingly
                                  return r.Result;
                              });
    }
}

I then inserted it into the HttpConfiguration to make sure it is the first/last handler to be hit. The way handlers work in the Web API is in a hierarchical fashion. So the first handler to be hit per request, will be the last handler to be hit on the response. At least this is my understanding, someone feel free to correct me if I m wrong.

public static void ConfigureApis(HttpConfiguration config)
{
    config.MessageHandlers.Insert(0, new PrincipalHandler());
}

通过采用这一方法,我们现在可以检查从网络信息预报和管制人员回复中得出的每一项结果。 这使得我们能够处理任何可能由于我们所期望的不归还而发生的伐木。 我们现在还可以改变回复的内容,以便国际独立调查局在发现某些《吉大港山区行动计划》的状态代码时,插入任何不实的超文本错误页。

The only problem I have with this, and I m hoping they change it in an upcoming release of the web API, is that they aren t sending the exception back up on the Task being returned from base.SendAsync(). So the only information we have to go by is the HTTP status code and try our best to give a reasonable or probable answer to the consumer.

问题回答

暂无回答




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签