如何在 asp.net mvc 3 项目中路由 .aspx 页面? [英] How to route a .aspx page in asp.net mvc 3 project?

查看:18
本文介绍了如何在 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

我该怎么做?

我试试这个:

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

但是我收到了 404 错误.

But i got the 404 error.

我做错了什么?

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

Obs: I put that before the Default route.

推荐答案

已解决! 所以,我们需要在 webforms 路由中添加一个路由约束,以确保它只捕获传入的路由,而不是输出路由生成.

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;
    }
}

然后将 Tickets 路由更改为以下内容:

Then change the Tickets route to the following:

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

这篇关于如何在 asp.net mvc 3 项目中路由 .aspx 页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆