locked
setting up 404 page error RRS feed

  • Question

  • User1519602350 posted

    How to setup 404 page error without showing error page. Any code needs to be added in the web.config?

    Thanks

    Wednesday, August 21, 2013 3:53 AM

Answers

  • User-1980594115 posted

    To show a custom-made error page instead of ANY server-side error page, then the customErrors mode needs to be On or RemoteOnly.

    <!-- Mode = On / Off / RemoteOnly -->
    <customErrors mode="On" defaultRedirect="~/errorPages/error.cshtml">
    	<error statusCode="404" redirect="~/errorPages/404.cshtml" />
    </customErrors>

    Basically this is how the following modes function:

    <customErrors mode="Off"/> will allow you to see details about the error.

    <customErrors mode="On"/> will send you to the defined custom page within customErrors.

    <customErrors mode="RemoteOnly"/> will perform just like "On" when you connect from a remote machine (not localhost). This is most useful because you don't have to worry about forgetting to change it when you deploy the app but can still see the error details while developing.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 21, 2013 6:48 PM
  • User-1635195291 posted

    Hi ssvikramuk,

    Yes, it is possible to get the URL that causes the 404 error, you just need to make sure you have IIS configured properly.

    There are two cases you need to handle, one is where the error comes from an .aspx or other page that is handled by .NET, and the other is where the error comes from a bad folder or filename (for example, *.htm) that is not handled by .NET. In IIS 7, you'll need to configure a custom 404 error under ".NET Error Pages" in the "ASP.NET" section for your web app, and also under "Error Pages" in the "IIS" section. The web.config changes end up looking something like this:

    <system.web>
        <!-- other system.web stuff -->
        <customErrors defaultRedirect="/Error404.aspx" mode="On" redirectMode="ResponseRewrite">
            <error redirect="/Error404.aspx" statusCode="404" />
        </customErrors>
    </system.web>
    <system.webServer>
        <!-- other system.webServer stuff -->
        <httpErrors errorMode="Custom">
            <remove statusCode="404" subStatusCode="-1" />
            <error statusCode="404" prefixLanguageFilePath="" path="/Error404.aspx" responseMode="ExecuteURL" />
        </httpErrors>
    </system.webServer>

    Note: the redirectMode="ResponseRewrite" listed above is important if you want your 404 pages to actually return 404 messages and I don't think it can be set through IIS.

    In my example, I created a page called Error404.aspx to handle all of the 404 errors. When a .NET page (.aspx, etc) throws a 404 exception, the original filename can be found in the aspxerrorpath querystring variable. When a regular htm or other page causes a 404 error, the original path can be read from the Request.RawUrl property. I used the following code in my Error404.aspx page to handle either case:

    public partial class Error404 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            OriginalUrl = Request.QueryString["aspxerrorpath"] ?? Request.RawUrl;
            Server.ClearError();
            Response.Status = "404 not found";
            Response.StatusCode = 404;
        }
    
        public string OriginalUrl { get; private set; }
    }

    By default, the 404 error page will not return a 404 status code, so you need to set it manually. See this post for more detail.

    Hope this helps.

    Thanks,

    Jatin



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 21, 2013 9:37 PM

All replies

  • User-18289217 posted
    <customErrors mode="On" defaultRedirect="~/DefaultError.cshtml" />
        <error statusCode="500" redirect="~/Error500.cshtml" />
        <error statusCode="404" redirect="~/Error404.cshtml" />
    </customErrors>

    Of course the Errorxxx.cshtml files should exist in your project

    Wednesday, August 21, 2013 4:54 AM
  • User1308270069 posted
    <customErrors defaultRedirect="~/Errors/Error.htm" mode="Off">
          <error statusCode="500" redirect="~/Errors/error.htm" />
          <error statusCode="404" redirect="~/Errors/filenotfound.htm" />
          <error statusCode="403" redirect="~/Errors/forbidden.htm" />
          <error statusCode="400" redirect="~/Errors/400badrequest.htm" />
          <error statusCode="401" redirect="~/Errors/401Unauthorizederror.htm" />
        </customErrors>

    Wednesday, August 21, 2013 6:39 AM
  • User-1980594115 posted

    To show a custom-made error page instead of ANY server-side error page, then the customErrors mode needs to be On or RemoteOnly.

    <!-- Mode = On / Off / RemoteOnly -->
    <customErrors mode="On" defaultRedirect="~/errorPages/error.cshtml">
    	<error statusCode="404" redirect="~/errorPages/404.cshtml" />
    </customErrors>

    Basically this is how the following modes function:

    <customErrors mode="Off"/> will allow you to see details about the error.

    <customErrors mode="On"/> will send you to the defined custom page within customErrors.

    <customErrors mode="RemoteOnly"/> will perform just like "On" when you connect from a remote machine (not localhost). This is most useful because you don't have to worry about forgetting to change it when you deploy the app but can still see the error details while developing.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 21, 2013 6:48 PM
  • User-1635195291 posted

    Hi ssvikramuk,

    Yes, it is possible to get the URL that causes the 404 error, you just need to make sure you have IIS configured properly.

    There are two cases you need to handle, one is where the error comes from an .aspx or other page that is handled by .NET, and the other is where the error comes from a bad folder or filename (for example, *.htm) that is not handled by .NET. In IIS 7, you'll need to configure a custom 404 error under ".NET Error Pages" in the "ASP.NET" section for your web app, and also under "Error Pages" in the "IIS" section. The web.config changes end up looking something like this:

    <system.web>
        <!-- other system.web stuff -->
        <customErrors defaultRedirect="/Error404.aspx" mode="On" redirectMode="ResponseRewrite">
            <error redirect="/Error404.aspx" statusCode="404" />
        </customErrors>
    </system.web>
    <system.webServer>
        <!-- other system.webServer stuff -->
        <httpErrors errorMode="Custom">
            <remove statusCode="404" subStatusCode="-1" />
            <error statusCode="404" prefixLanguageFilePath="" path="/Error404.aspx" responseMode="ExecuteURL" />
        </httpErrors>
    </system.webServer>

    Note: the redirectMode="ResponseRewrite" listed above is important if you want your 404 pages to actually return 404 messages and I don't think it can be set through IIS.

    In my example, I created a page called Error404.aspx to handle all of the 404 errors. When a .NET page (.aspx, etc) throws a 404 exception, the original filename can be found in the aspxerrorpath querystring variable. When a regular htm or other page causes a 404 error, the original path can be read from the Request.RawUrl property. I used the following code in my Error404.aspx page to handle either case:

    public partial class Error404 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            OriginalUrl = Request.QueryString["aspxerrorpath"] ?? Request.RawUrl;
            Server.ClearError();
            Response.Status = "404 not found";
            Response.StatusCode = 404;
        }
    
        public string OriginalUrl { get; private set; }
    }

    By default, the 404 error page will not return a 404 status code, so you need to set it manually. See this post for more detail.

    Hope this helps.

    Thanks,

    Jatin



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 21, 2013 9:37 PM