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

Vin*_*oni 14 c# asp.net asp.net-mvc routing

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

Areas/Management/Views/Ticket/Report.aspx
Run Code Online (Sandbox Code Playgroud)

我想在浏览器中将其路由到以下路径:

http://localhost/Reports/Tickets
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

我试试这个:

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

但我得到了404错误.

我做错了什么?

Obs:我把它放在Default路线之前.

Chr*_*ver 22

如果你试图在MVC项目中使用Web表单,那么我会将你的.aspx移出views文件夹,因为它实际上不是一个视图,所以像WebForms/Tickets/Report.aspx.

在Web表单中,您可以通过调用MapPageRoute方法来映射路由.

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

你需要把它放在默认的MVC路由之前.


Vin*_*oni 13

解决了!因此,我们需要在webforms路由中添加路由约束,以确保它只捕获传入路由,而不是传出路由生成.

将以下类添加到项目中(在新文件或global.asax.cs的底部):

public class MyCustomConstaint : IRouteConstraint{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection){
        return routeDirection == RouteDirection.IncomingRequest;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后将故障单路由更改为以下内容:

routes.MapPageRoute(
    "Tickets",
    "Reports/Tickets",
    "~/WebForms/Reports/Tickets.aspx",
    true, null, 
    new RouteValueDictionary { { "outgoing", new MyCustomConstaint() } }
);
Run Code Online (Sandbox Code Playgroud)

  • 感谢CodeHobo提供此解决方案.[参考文献](http://forums.asp.net/t/1793416.aspx/1) (6认同)