httpmodules httphandlers, whats the ideal use of them? when to use and when not to use?

前端 未结 6 851
清酒与你
清酒与你 2021-02-04 13:36

I have some questions about httpmodules and httphandlers, i am a little confused of the real need of them, i created many websites, but rarely used them, sure i lost a benefit f

相关标签:
6条回答
  • 2021-02-04 14:17

    Both handlers and modules can only really be understood in the context of the ASP.NET pipeline model and how a custom handler/module you create will fit into that model.

    Couple of good resources here and here.

    As others have mentioned modules can be used to create behaviour at certain event points (another common use is global exception handling), handlers create endpoint request handling (it's not just a clever name) where an asp.net page instance (which ultimately is just a heavily wrapped up handler anyway) would be either unnecessarily heavy or outright wrong. Personally I use any time I'm creating something which doesn't output HTML, which gets more and more common the more you develop web-apps as opposed to web-sites.

    0 讨论(0)
  • 2021-02-04 14:18

    HttpHandlers are great for re-routing requests for certain file types and other resources. For example, when you need to do something with an incoming request for a file type that ASP.NET doesn't protect via FormsAuthentication, you can actually determine if it is a particular file type, located in a specific directory and redirect the user to another page if not authorized.

    HttpModules can perform specific processing for your app in a resource-independent fashion. A more technical explanation can be found here: http://www.15seconds.com/issue/020417.htm

    Also, they do add overhead to each request, which means you shouldn't use them for process-intensive tasks.

    0 讨论(0)
  • 2021-02-04 14:19

    I will quote the usage, people have already given good definitions.

    HTTP handlers
    Imaging you want to render the sitemap of a website to search engine crawler, rather than popping up the file save dialog (The file save dialog appears when ASP.NET/IIS do not have a default handler for the file type requested, ex. txt). I wrote an HTTP Handler to construct sitemap files on the fly (pulling sitemap info from DB).

    0 讨论(0)
  • 2021-02-04 14:25

    Have you found yourself writing code in an ASPX Page_Load event to emit something to the response and then calling Response.End? That was likely best written as a HttpHandler (ashx).

    0 讨论(0)
  • 2021-02-04 14:26

    you could use httpmodules for authentication, auditing or url-rewriting. Since every request first passes through all modules, before it's handled by the httphandler, this is the place to apply code that you want to run before your specific application code.

    You would use an httphandler if you don't want to rely on the asp.net engine. for example you could write your own rendering mechanism based on AJAX, XML and XSLT which does not rely on traditional asp.net pages.

    0 讨论(0)
  • 2021-02-04 14:27

    HttpModules and HttpHandlers are useful when you wish to change IIS's default handling of documents either by changing the behavior entirely or adding pre or post processing to the document.

    One practical application I've had for an HttpModule is for dynamic thumbnail creation. I wrote an HttpModule that handled all image requests. If the request contained the query string "?thumb", I'd dynamically generate a thumbnail rather than serving up the original image.

    I've also seen HttpModules used to remove comments and whitespace from aspx page output and url rewriting.

    0 讨论(0)
提交回复
热议问题