开发者

How to Reach the Custom Errors page in ASP.NET when Session State Provider is Down?

开发者 https://www.devze.com 2023-03-15 04:50 出处:网络
I am trying to show a customer errors page in ASP.NET when the database is down. I use the SQL S开发者_JS百科erver mode to hold the session data. The problem is that the custom errors page is never ca

I am trying to show a customer errors page in ASP.NET when the database is down. I use the SQL S开发者_JS百科erver mode to hold the session data. The problem is that the custom errors page is never called.

Since the session data and the database are on the same server, this does not redirect to the custom error page? I’m guessing the web application has not loaded at this point?. The user is presented with the stack trace for the session state connection failure.

It seems that we need something that sits in front of the initial website load to check connectivity to the database. Any ideas on how to implement this?


Add something like this to your web.config?

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
    <error statusCode="403" redirect="NoAccess.htm" />
    <error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>

You can read more information here

If it is your SqlSessionState that is failing, you should handle the corresponding error in the Application_Error event in Global.asax

You can read more information here


I believe the error is coming from the fact that because you're using an out of memory session state provider (being the database), and the database connection has a failure, then there is actually a perceived error in the web configuration (not in the application). I have a similar problem, where I'm using AppFabric Cache for my session state provider, but when the AppFabric Cache Service is down, I get the Configuration Error page.

Because of this, you can't use the customErrors solution as FlyingStreudel has already suggested, since it isn't an error in your application, but rather in the loading of the configuration.

I've looked around for a way to solve this, but couldn't find any. I hope this question gets answered, it's got me confused already with the various error configuration options...

Update: After investigating this for a while now, it appears that my issue comes from the fact that the SessionStateModule causes the AppFabric cache session state provider to try and connect to the DataCache (which is not available), and an exception (probably timeout) is thrown somewhere. Because this happens in the Init of the HTTP module, there seems to be no way around the yellow screen of death.

I wouldn't be surprised if the original poster's problem is the same - the connection to the SQL server occurring in the initialization of the SessionStateModule.


Because the error page is an ASP.NET Page ( I can see this from your comment), It will hit the session database in page life cycle. You have to set below directive on Error.aspx Page to tell ASP.Net not to load session information for it:

EnableSessionState="false"

Please note this will only work if you are not using any session information in the error page.

Additionally, I also managed Global.asax page as below :

    private static Exception startException;
     protected void Application_Start()
            {
                try
                {
                    AreaRegistration.RegisterAllAreas();
                    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                    RouteConfig.RegisterRoutes(RouteTable.Routes);
                    BundleConfig.RegisterBundles(BundleTable.Bundles);
                    ModelBinders.Binders.Add(typeof(DateTime), new MyDateTimeModelBinder());

                }
                catch (Exception ex)
                {
                    startException = ex;
                }
            }
  static HashSet<string> allowedExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
    ".js", ".css", ".png",".jpg",".jpeg",".gif",".bmp"
};

        public bool IsStaticFile(HttpRequest request)
     {  //My project was a mix of asp.net web forms & mvc so had to write this      
                if (Request.RawUrl.ToLower().Contains("/bundles/") || Request.RawUrl.ToLower().Contains("/frontendcss?") ||
                     Request.RawUrl.ToLower().Contains("/fonts/") 
//these are my css & js bundles. a bit of hack but works for me.
                     )
                {
                return true;
            }
            string fileOnDisk = request.PhysicalPath;
            if (string.IsNullOrEmpty(fileOnDisk))
            {
                return false;
            }

            string extension = Path.GetExtension(fileOnDisk).ToLower();

            return allowedExtensions.Contains(extension);
        }
     protected void Application_BeginRequest()
            {
                if (startException != null && Request.RawUrl.ToLower() == "/Error.aspx".ToLower())
                {
                    return;
                }
                if (startException != null && IsStaticFile(Request))
                {
                    return;
                }

                if (startException != null && Request.RawUrl.ToLower()!= "/Error.aspx".ToLower())
                {
                    //Response.Redirect("Error.aspx");
                    Server.Transfer("Error.aspx");
                    this.Context.AddError(startException);                
                    return;
                }


            }
        protected void Application_Error(object sender, EventArgs e)
                {
                   Exception exception = Server.GetLastError();
                     Response.Clear();
                    Server.ClearError(); 
                   Server.Transfer("Error.aspx");     
                }
0

精彩评论

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