Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
659 views
in Technique[技术] by (71.8m points)

c# - After add MapPageRoute to an asp.net mvc project, the site stops to enter in Home Controller

I'm trying to route a .aspx (webforms page) in my asp.net mvc project. I register the page in global.asax:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("Tickets", "Reports/Tickets", "~/WebForms/Reports/Tickets.aspx");
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });

The problem is, after i add the second line, the site stops to enter in my Home Controller (Index Action) and is redirecting to: http://localhost:37538/Reports/Tickets?action=Index&controller=Login%22 always that i run the project.

Project Details:

  • Asp.Net MVC 3
  • Forms Authentication
  • .Net 4.0

Obs: to reproduce this error, create a new asp.net mvc project as internet app, after create the Tickets webforms page inside a /WebForms/Reports folder, and register the new route. Run the project (probably you're logged), so now logoff and you will be redirected to http://localhost:35874/Reports/Tickets?action=LogOff&controller=Account, so why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Solved! So, we need to add a route contraint to the webforms route to ensure that it only catches on incoming routes, not outgoing route generation.

Add the following class to your project (either in a new file or the bottom of global.asax.cs):

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

Then change the Tickets route to the following:

routes.MapPageRoute(
    "Tickets",
    "Reports/Tickets",
    "~/WebForms/Reports/Tickets.aspx",
    true, null, 
    new RouteValueDictionary { { "outgoing", new MyCustomConstraint() } }
);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...