After add MapPageRoute to an asp.net mvc project, the site stops to enter in Home Controller(将 MapPageRoute 添加到 asp.net mvc 项目后,站点停止进入 Home Controller)
问题描述
我正在尝试在我的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 MapPageRoute 添加到 asp.net mvc 项目后,站点停止


- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 输入按键事件处理程序 2022-01-01