locked
Problem while redirecting to CustomError Page RRS feed

  • Question

  • User-614943948 posted

    I am using this source code to remove the extension names from the webpage browsed by user.

    RouteConfig.cs file

    using Microsoft.AspNet.FriendlyUrls;
    using System.Web.Routing;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    /// Summary description for RouteConfig
    /// </summary>
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            var settings = new FriendlyUrlSettings();
            settings.AutoRedirectMode = RedirectMode.Permanent;
            routes.EnableFriendlyUrls(settings);
        }
    }

    Now I added this line in Global.asax

     void Application_Start(object sender, EventArgs e) 
        {
            // Code that runs on application startup
    	RouteConfig.RegisterRoutes(System.Web.Routing.RouteTable.Routes);
        }

    It works fine. But its not letting my redirect to a custom error page, which I defined in web.config like this...

    <customErrors mode="On" defaultRedirect="~/CustomError.aspx">
          <error statusCode="404" redirect="~/CustomError.
    </customErrors>

    I have to comment this line in global.ascx

    RouteConfig.RegisterRoutes(System.Web.Routing.RouteTable.Routes);

    for the custom error to work

    How can I make it work?

    Wednesday, February 10, 2021 11:54 AM

All replies

  • User1535942433 posted

    Hi maverick786us,

    As far as I think,you could do like this:

    global.asax.cs:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }

    Web.config:

    <customErrors defaultRedirect="~/CustomError" mode="On">
    </customErrors>

    Best regards,

    Yijing Sun

    Thursday, February 11, 2021 6:17 AM
  • User-614943948 posted

    Hi maverick786us,

    As far as I think,you could do like this:

    global.asax.cs:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }

    Web.config:

    <customErrors defaultRedirect="~/CustomError" mode="On">
    </customErrors>

    Best regards,

    Yijing Sun

    Thank you Sun, that application is developed in ASP.NET not MVC

    Thursday, February 11, 2021 7:21 AM
  • User1535942433 posted

    Hi maverick786us,

    According to your description,as far as I think,if you need to use custom error,you need to remove http error first.Just like this:

    <configuration>
        ...
        <system.web>
            ...
            <customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Error/500.htm">
                <error statusCode="404" redirect="~/Error/404.htm" />
                <error statusCode="500" redirect="~/Error/500.htm" />
            </customErrors>
            ...
        </system.web>
        ...
        <system.webServer>
            ...
            <httpErrors errorMode="Custom">
                <remove statusCode="404" />
                <error statusCode="404" path="/Error/404.htm" responseMode="ExecuteURL" prefixLanguageFilePath="" />
                <remove statusCode="500" />
                <error statusCode="500" path="/Error/500.htm" responseMode="ExecuteURL" prefixLanguageFilePath="" />
            </httpErrors>
            ...
         </system.webServer>
         ...
    </configuration>

    Best regards,

    Yijing Sun

    Tuesday, February 16, 2021 9:44 AM