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 项目后,站点停止
基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01