HttpHandler fire only if file doesn't exist

后端 未结 3 1066
执念已碎
执念已碎 2021-02-09 09:35

I\'m trying to create a HTTP handler to handle all requests to a folder but I only want it to fire if the files requested don\'t exist (EG: Request comes in for file X, if X exi

3条回答
  •  后悔当初
    2021-02-09 10:14

    I've ended up sticking with a handler and instead using the following to solve the problem:

    if (File.Exists(context.Request.PhysicalPath)) context.Response.TransmitFile(context.Request.PhysicalPath);
    else { /* Standard handling */ }
    

    Given so many people advocated Modules and catching exceptions I feel I should clarify why I didn't listen:

    1. This is standard program flow, as such I don't like the idea of using an exception to trigger it unless it becomes absolutely necessary.
    2. This is actually returning content under normal circumstances. The idea of a HttpModule actually handling typical requests rather than just doing some basic shared processing and handling edge-cases seems a bit off. As such I prefer to use a HttpHandler as it is handling the typical requests.

提交回复
热议问题