MapPageRoute fails to redirect to specified aspx page

161 Views Asked by At

I have the following in my RouteConfig.cs file

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

            // route default URL to index.aspx
            routes.MapPageRoute(
                routeName: "LoginPageRoute",
                routeUrl: "login",
                physicalFile: "~/Login.aspx"
            );

            routes.MapPageRoute("LoginPageRoute2", "login2", "~/Login.aspx");


        }

However if I try to access my WebApp using "login" or "login2" I get a resource cannot be found error message.

  • http://localhost:4200/login2 - fails
  • http://localhost:4200/login - fails
  • http://localhost:4200/Login.aspx - loads fine

My Global.asax has the following

 protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
1

There are 1 best solutions below

0
elspoono On

A comment from somebody under my question guided me to the solution which was to move my custom routes ahead of the default routes.

Adding the following to Global.asax helped prove that this was indeed the issue.


        public override void Init()
        {
            base.Init();
            this.AcquireRequestState += showRouteValues;
        }

        protected void showRouteValues(object sender, EventArgs e)
        {
            var context = HttpContext.Current;
            if (context == null)
                return;
            var routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(context));
        }

Putting a breakpoint on

var routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(context));

Showed that my "login" mapping was using the default route mapping instead of my custom route mapping.

Thanks again @mason!