MapPageRoute 中断了集成 MVC/WebForms 应用程序中的 ActionLinks [英] MapPageRoute Breaks ActionLinks in Integrated MVC/WebForms App

查看:21
本文介绍了MapPageRoute 中断了集成 MVC/WebForms 应用程序中的 ActionLinks的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 ASP.NET 4.0 中开发的现有 Web 应用程序.我想向应用程序添加 MVC 功能,因此我按照 Scott Hanselman 的文章将 MVC 集成到应用程序中 将 ASP.NET MVC 3 集成到现有升级的 ASP.NET 4 Web 窗体应用程序中.因为MVC路由是贪婪的,所以我在我的Global.asa中添加了以下代码,这样一个空的URL就会转到我的Default.aspx:

I have an existing Web application that was developed in ASP.NET 4.0. I want to add MVC functionality to the app, so I've integrated MVC into the app as per Scott Hanselman's article Integrating ASP.NET MVC 3 into existing upgraded ASP.NET 4 Web Forms applications. Because MVC routing is greedy, I added the following code to my Global.asa so that an empty URL will go to my Default.aspx:

routes.MapPageRoute("WebFormsDefault", "", "~/Default.aspx");

现在的问题是 ActionLinks 和 RouteLinks 的格式不正确.如果我尝试使用以下方法创建操作链接:

The problem now is that ActionLinks and RouteLinks don't form correctly. If I try to create an action link using:

@Html.ActionLink("Item List Page", "List", "Item")

创建以下 URL:

"/SiteName/?action=List&controller=Item

我从其他人那里找到了几个有同样问题的帖子,但没有一个有任何答案.这只是一个错误吗?一般而言,将 MVC 集成到 WebForms 应用程序只是一个坏主意吗?或者有没有办法解决这个问题,以便在用户第一次进入站点时显示我的 Default.aspx 页面并且 ActionLinks 和 RouteLinks 将正常工作?

I've found several posts from others with this same problem, but none of them have any answer. Is this just a bug? Is integrating MVC into a WebForms app just a bad idea in general? Or is there a way to fix this so that my Default.aspx page will be displayed when a user first enters the site and ActionLinks and RouteLinks will work correctly?

推荐答案

来得有点晚,但我觉得迟到总比没有好.我遇到了完全相同的问题,我通过将我的 MapPageRoute 代码和我的 MapRoute 代码分组,然后总是首先调用 MapRoute 代码来解决它.示例:

Coming to this a bit late, but I figure better late than never. I was having this exact same issue and I solved it by grouping my MapPageRoute code and my MapRoute code and then always calling the MapRoute code first. Example:

本来我的路由是这样的 -

Originally my routing looked like this -

routes.MapPageRoute("401", "401/", "~/Views/Error/401.aspx");
routes.MapPageRoute("404", "404/", "~/Views/Error/404.aspx");
routes.MapPageRoute("500", "500/", "~/Views/Error/500.aspx");
routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = "" }
            );

等等等等

这导致我的所有表单操作都指向格式如下的 URL:/mysite/401?action=x&controller=y

This was causing all of my form actions to direct to a url formatted as so: /mysite/401?action=x&controller=y

显然这没有用.通过确保我总是首先设置我的所有 MVC 路由,问题自行解决.我最终制作了两种单独的方法,一种用于配置 MVC 路由,一种用于配置 Webform 路由,如下所示:

Clearly that was not useful. By making sure that I always setup all of my MVC routes first, the problem resolved itself. I ended up making two seperate methods, one for configuring MVC routes and one for configuring Webform routes as so:

RouteConfig.RegisterMvcRoutes(RouteTable.Routes);  // contains only MapRoute
RouteConfig.RegisterWFRoutes(RouteTable.Routes);  // contains only MapPageRoute

(这些调用照常进入 Global.asax 文件并替换 RouteConfig.RegisterRoutes)

(these calls go into the Global.asax file as usual and replace RouteConfig.RegisterRoutes)

这篇关于MapPageRoute 中断了集成 MVC/WebForms 应用程序中的 ActionLinks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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