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

After add MapPageRoute to an asp.net mvc project, the site stops to enter in Home Controller(将 MapPageRoute 添加到 asp.net mvc 项目后,站点停止进入 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

MSBuild cannot find a reference(MSBuild 找不到参考)
The reference assemblies for framework .NETCore, Version=v5.0 were not found(未找到框架 .NETCore,Version=v5.0 的参考程序集)
quot;File has a different computed hash than specified in manifestquot; error when signing the EXE(“文件的计算哈希值与清单中指定的不同签署EXE时出错)
MS-Build BeforeBuild not firing(MS-Build BeforeBuild 未触发)
Using C# 7.1 with MSBuild(将 C# 7.1 与 MSBuild 结合使用)
Build project with Microsoft.Build API(使用 Microsoft.Build API 构建项目)