开发者

Can GetHandler return null when implementing IHttpHandlerFactory?

开发者 https://www.devze.com 2023-03-25 17:14 出处:网络
Will this code throw an exception: public class MyHttpHandlerFactory : IHttpHandlerFactory { public IHttpHandler GetHandler(H开发者_如何学PythonttpContext context, string requestType, string url, str

Will this code throw an exception:

public class MyHttpHandlerFactory : IHttpHandlerFactory
{
    public IHttpHandler GetHandler(H开发者_如何学PythonttpContext context, string requestType, string url, string pathTranslated)
    {
        if (...)
            return null;

            ...

        return new MyHttpHandler();
    }
}


If you take a look at this article on MSDN, you'll see that in their example they return null from GetHandler. However, they only return null if the request is not a GET or a POST, which should never happen based on the way they have set up the factory in the web.config.

I set up a quick sample using the code from the article using ASP.NET 4.0/IIS 7.5/Integrated Pipeline and if you do return null from a call to GetHandler all that appears to happen is an empty 200/OK response is returned from the server (I checked using Fiddler). So it seems that ASP.NET 4.0 (at least) handles this condition gracefully. To answer the question as asked, no it does not appear that there is a runtime limitation to returning null. In practice, however, you may want to limit the requests that your HandlerFactory is receiving so that it never returns null, or at least consider how the other parts of your application will react to a request to this handler factory returning an empty 200/OK response.

0

精彩评论

暂无评论...
验证码 换一张
取 消