将 MapPageRoute 添加到 asp.net mvc 项目后,站点停止

时间:2023-05-07
本文介绍了将 MapPageRoute 添加到 asp.net mvc 项目后,站点停止进入 Home Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试在我的 asp.net mvc 项目中路由一个 .aspx(webforms 页面).我在 global.asax 中注册页面:

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

问题是,在我添加第二行之后,站点停止进入我的主控制器(索引操作)并重定向到:http://localhost:37538/Reports/Tickets?action=Index&controller=Login%22 总是我运行项目.

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.

项目详情:

  • Asp.Net MVC 3
  • 表单验证
  • .Net 4.0

Obs:要重现此错误,请在 /WebForms/Reports 文件夹中创建 Tickets webforms 页面后,创建一个新的 asp.net mvc 项目作为 Internet 应用程序,并注册新路线.运行项目(可能您已登录),现在注销,您将被重定向到 http://localhost:35874/Reports/Tickets?action=LogOff&controller=Account,为什么?

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?

推荐答案

解决了! 所以,我们需要在 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 MyCustomConstraint : 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 MyCustomConstraint() } }
);

这篇关于将 MapPageRoute 添加到 asp.net mvc 项目后,站点停止进入 Home Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:ASP.NET MVC:如何使用 .(期间)结束 下一篇:asp.net 4.0 网络表单路由 - 默认/通配符路由

相关文章

最新文章