locked
Is it possible to serve a precompiled .htm from a HttpModule Assembly in referencing web app? RRS feed

  • Question

  • User-1002157272 posted

    I have an HttpModule I'm building as an assembly. It will be included in multiple different web applications and I would like to implement the ability to provide a "default" static html page (.htm) file that could be served in response to certain page requests.

    Basically I would like this implementation to be able to return the static html page from the referenced assembly within the request handler. Essentially it should act like a normal response.redirect.

    Is this possible? And if so, how do I go about doing so? I don't want it to act in a way that the user "downloads the page like a file download, but rather, that it would just display in the browser window like a normal page request.

    Monday, June 11, 2012 9:58 PM

Answers

  • User1779161005 posted

    Your module can bundle the static html as an embedded resource into the assembly and then you can simply open that and stream it to the response. Something like this (pseudocode):

    Assembly a = typeof(YourModule).Assembly;
    using (var stream = a.GetManifestResourceStream("YourNamespace.YourFile.htm"))
    {
        HttpContext.Response.ContentType = "text/html";
        stream.CopyTo(HttpContext.Response.OutputStream);
    }
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 11, 2012 10:34 PM

All replies

  • User1779161005 posted

    Your module can bundle the static html as an embedded resource into the assembly and then you can simply open that and stream it to the response. Something like this (pseudocode):

    Assembly a = typeof(YourModule).Assembly;
    using (var stream = a.GetManifestResourceStream("YourNamespace.YourFile.htm"))
    {
        HttpContext.Response.ContentType = "text/html";
        stream.CopyTo(HttpContext.Response.OutputStream);
    }
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 11, 2012 10:34 PM
  • User-1002157272 posted

    You are soooo going on my friends list Brock!!! lol Thank you again. One question though, if I want this page to override whatever page was originally requested, do I have to do a context.response.ClearContent() before copying the file to the response? Or should it overwrite it automatically?

    Monday, June 11, 2012 10:50 PM
  • User1779161005 posted

    Oh right, yes -- you'd also need a Response.End() in there, and maybe a Response.Clear() before (to remove anything else that might have already been written, yes).

    Monday, June 11, 2012 10:57 PM