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

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

问题描述

我有一个在ASP.NET 4.0中开发的现有Web应用程序.我想向应用程序中添加MVC功能,因此我已经按照Scott Hanselman的文章将ASP.NET MVC 3集成到现有的升级的ASP.NET 4 Web Forms应用程序中.由于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")

以下网址已创建:

"/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天全站免登陆