URL Routing C# mvc and Web Forms

2019-03-04 03:33发布

问题:

So I have a webforms and an mvc application combined and am trying to get things routed correctly. I have the default routing working as expected, but when I click on an actionlink in one of my views, it is not routing to the correct page.

Here's my routing code.

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

           routes.MapPageRoute("",
               "", "~/Default.aspx", true);

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



        }


        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup

           AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();

        }

Here's an Action Link that I would click on:@Html.ActionLink("Properties Editor", "Index", "Property")

Here's my expected Result: urlgoeshere.com/Property/Index

Here's my actual Result:urlgoeshere.com/?action=Index&controller=Property

I'm not sure what to change to remedy this situation? Any ideas?

回答1:

I ended up having to add a routing constraint. Here's what I ended up doing.

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

          routes.MapPageRoute("",
               "", "~/Default.aspx", true, null, new RouteValueDictionary { { "outgoing", new PageConstraint() } });


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

And the page constraint.

public class PageConstraint : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            if (routeDirection == RouteDirection.IncomingRequest)
                return true;
            return false;
        }
    }


回答2:

I think it might be because the first route that matches is the Default.aspx route so it uses that, with the URL "". In MVC any parameters provided but not included in the path are automatically added as querystring values.

Routes are picked up in the order they are defined so try moving routes.MapPageRoute("", "", "~/Default.aspx", true); to be after the Default route. You might need to remove the controller default if you do this otherwise this route will get picked up for the URL "" which will pass to the Index action on the Chips controller.