如何路由在asp.net MVC 3项目中的.aspx页面中?

问题描述:

我有一个.aspx页以下路径:

I have a .aspx page in the following path:

Areas/Management/Views/Ticket/Report.aspx

我要路由到我的浏览器以下路径:

I want to route that to the following path in my browser:

http://localhost/Reports/Tickets

我怎样才能做到这一点?

How can i do that?

我试试这个:

routes.MapRoute(
    "Tickets", // Route name
    "Areas/Management/Views/Ticket/Report.aspx", // Original URL
    new { controller = "Reports", action = "Tickets" } // New URL 
);

但我得到了 404 错误。

我在做什么错了?

观测:我把该默认路线之前

解决了!所以,我们需要一个路由约束实现添加到Web表单的路线,以确保它仅捕获传入的路线,不出局路由的产生。

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.

下面的类添加到项目中(无论是在一个新的文件或的global.asax.cs的底部):

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

public class MyCustomConstaint : 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 MyCustomConstaint() } }
);