Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since IIS7 uses .net from the top down there would not be any performance overhead of using an HttpModule, In fact there are several Managed HttpModules that are always used on every request. When the BeginRequest event is fired, the SessionStateModule may not have been added to the Modules collection, so if you try to handle the request during this event no session state info will be available. Setting the HttpContext.Handler property will initialize the session state if the requested handler needs it, so you can just set the handler to your fancy 404 page that implements IRequiresSessionState. The code below should do the trick, though you may need to write a different implementation for the IsMissing() method:</p> <pre><code>using System.Web; using System.Web.UI; class Smart404Module : IHttpModule { public void Dispose() {} public void Init(HttpApplication context) { context.BeginRequest += new System.EventHandler(DoMapping); } void DoMapping(object sender, System.EventArgs e) { HttpApplication app = (HttpApplication)sender; if (IsMissing(app.Context)) app.Context.Handler = PageParser.GetCompiledPageInstance( "~/404.aspx", app.Request.MapPath("~/404.aspx"), app.Context); } bool IsMissing(HttpContext context) { string path = context.Request.MapPath(context.Request.Url.AbsolutePath); if (System.IO.File.Exists(path) || (System.IO.Directory.Exists(path) &amp;&amp; System.IO.File.Exists(System.IO.Path.Combine(path, "default.aspx")))) return true; return false; } } </code></pre> <p>Edit: I added an implementation of IsMissing()</p> <p>Note: On IIS7, The session state module does not run globally by default. There are two options: Enable the session state module for all requests (see my comment above regarding running managed modules for all request types), or you could use reflection to access internal members inside System.Web.dll.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload