CustomErrors does not work when setting redirectMode=“ResponseRewrite”

前端 未结 10 1945
野趣味
野趣味 2020-11-27 11:51

In a old site, I was changing the way that CustomErrors works by adding redirectMode=\"ResponseRewrite\" (new in 3.5 SP1):



        
相关标签:
10条回答
  • 2020-11-27 12:37

    According to @Amila's post and confirmation and completion of that post, I have same problem, I dig a lot google but had no chance to find the correct answer. The problem is when you are working with ASP.Net Web Application, whether it's an MVC or not you can't achieve custom error using the old way with Webform project.
    Here the Option if you are using ASP.Net Web Application (whether it's an MVC or not):

    In my scenarios I just want to define a custom error for a specific 404 error, The other error defined same as 404 error:


    Senario1: Your custom page is a simple HTML file and placed in the root:

    <configuration>
       <system.web>
          <customErrors mode="Off" />
       </system.web>
       <system.webServer>
           <httpErrors errorMode="Custom" existingResponse="Replace">
               <remove statusCode="404" subStatusCode="-1" />
               <error statusCode="404" path="ErrorPage.html" responseMode="File" />
           </httpErrors>
       </system.webServer>
    </configuration>
    



    Senario2: Your custom page is an aspx page and placed in the root:

    <configuration>
       <system.web>
          <customErrors mode="Off" />
       </system.web>
       <system.webServer>
           <httpErrors errorMode="Custom" existingResponse="Replace">
               <remove statusCode="404" subStatusCode="-1" />
               <error statusCode="404" path="ErrorPage" responseMode="Redirect" />
           </httpErrors>
       </system.webServer>
    </configuration>
    

    Note: I remove the aspx extension due to RouteConfig.cs in ASP.net application, you can use ErrorPage.aspx if you like, it's optional.


    Senario3: Your custom page is an aspx page and placed in the [ex: Page folder in The root (~/Page/ErrorPage.aspx)]:
    The tip here that I noticed is YOU SHOULD NOT USE~/ to the root addressing; So I just addresing without ~/ mark:

    <configuration>
       <system.web>
          <customErrors mode="Off" />
       </system.web>
       <system.webServer>
           <httpErrors errorMode="Custom" existingResponse="Replace">
               <remove statusCode="404" subStatusCode="-1" />
               <error statusCode="404" path="Page/ErrorPage" responseMode="Redirect" />
           </httpErrors>
       </system.webServer>
    </configuration>
    
    0 讨论(0)
  • 2020-11-27 12:43

    In my particular case, my error page had a master page that had a user control that tried to use Session. If Session isn't available, you get an HttpException: "Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive." Easiest fix is to switch to static html, second easiest fix is to use a simpler error page, hardest fix is to make incredibly sure that your error page makes no assumptions anywhere (like that Session won't throw an exception, for example) and can't possibly error out.

    0 讨论(0)
  • 2020-11-27 12:46

    I have found out that if you use redirectMode="ResponseRewrite" then you need to add something in the rewrite area of the web.config file. Problem is when your site is broken! You can't URL rewrite as your site can't call the "virtual.aspx" that handles your rewrite!

    0 讨论(0)
  • 2020-11-27 12:52

    It is important to note for anyone trying to do this in an MVC application that ResponseRewrite uses Server.Transfer behind the scenes. Therefore, the defaultRedirect must correspond to a legitimate file on the file system. Apparently, Server.Transfer is not compatible with MVC routes, therefore, if your error page is served by a controller action, Server.Transfer is going to look for /Error/Whatever, not find it on the file system, and return a generic 404 error page!

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