@ Url.Action("Action","Controller")返回"/cgi-bin?action = Action& ampler = controller = Controller". [英] @Url.Action("Action", "Controller") returns "/cgi-bin?action=Action&controller=Controller"

查看:63
本文介绍了@ Url.Action("Action","Controller")返回"/cgi-bin?action = Action& ampler = controller = Controller".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,它认为目标是Apache服务器-我怀疑吗?

For some reason it thinks the target is an Apache server - I suspect?

MVC是V5.2.3,其依存关系根据 nuget是正确的.我已经搜索了,但搜索无济于事.

The MVC is V5.2.3 and its dependancies are correct as per nuget . I have searched and searched to no avail.

编辑代码就是 @ Url.Action("Action","Controller")

因此,我创建了一个全新的MVC项目,并使用完全相同的代码返回了正确的代码.

So I created a brand new MVC project and using exactly the same code the correct code was returned.

http://localhost:53143/Controller/Action

编辑2 由于问题不存在,我删除了web.config文件.

EDIT 2 I removed my web.config files as the problem is not there.

推荐答案

我与一位专家取得了联系,他看了看项目并回答如下.

I got in touch with an expert and he looked at the project and answered as below.

(顺便说一句,我的路由中有'cgi-bin',因为有一些旧的URL狂野地与我以前托管在Apache服务器上的域有关,我无法更改.)

(BTW I have 'cgi-bin' in a route as there are old URLs out in the wild that relate to my domain previously being hosted on an Apache server and which I cannot change.)

答案我进一步研究了mvc帮助器的源代码,是的,这是两个问题(我遇到了类似的问题)是相关的,因为Url.Action和Html.BeginForm归结为调用一个相同的方法:UrlHelper.GetUrl ...现在,此方法的作用是:

The answer I digged a little more into the source code of the mvc helpers and, yes, the two issues (I had a similar problem Html.BeginForm with overload causes form action to be '/cgi-bin?action=Index&controller=Home' and so the HomeController is bypassed) are related since Url.Action and Html.BeginForm boil down to calling one and the same method: UrlHelper.GetUrl... Now, what this method does is:

  1. 检索当前URL,包括控制器,操作,区域...
  2. 添加或替换您指定的参数,
  3. 找到最佳匹配路线!< ==
  4. 如果有任何路由变量-将提供的参数推送到这些变量中.
  5. 将其余参数粘贴到查询字符串中< ==

我特意强调了第3点和第2点.5,第3点是最重要的.因此,UrlHelper.GetUrl(分别为Url.Action和Html.BeginForm)需要一条路由,并搜索可用的路由以找到第一个匹配项.

I have deliberately highlighted point 3 & 5, with point 3 being the most important. So, UrlHelper.GetUrl (and Url.Action and Html.BeginForm respectively) needs a route and it searches through the available ones to find the first match.

现在,混合的Webforms-mvc应用程序出现了问题-当您纯粹使用MVC处理时,这个问题不存在:您正在使用MapPageRoute!请注意,它与MapRoute不同.然后,MapPageRoute使用PageRouteHandler类创建路由,而MapRoute使用MvcRouteHandler类,并且两者有所不同,因为PageRouteHandler以始终与UrlHelper.GetUrl("ActionName","ControllerName")匹配的方式创建路由将操作名称和控制器名称作为参数放入查询字符串中(第5点).

Now, here comes the problem with your mixed webforms-mvc app - an issue which is not present when you are dealing purely with MVC: You are using MapPageRoute!!! Please, note that it is different from MapRoute. And MapPageRoute uses the PageRouteHandler class to create the route whereas MapRoute uses the MvcRouteHandler class and it makes all the difference because PageRouteHandler creates the route in such a way that it's always a good match for UrlHelper.GetUrl("ActionName", "ControllerName") with the action name and controller name being thrown into the query string as parameters (point 5).

因此,您的设置会发生什么情况,即Url.Action正在搜索一条路线,并且碰到了MapPageRoute创建的第一个路线,在您的情况下是:

So, what happens with your set-up is that Url.Action is searching for a route and is hitting the first one created by MapPageRoute and in your case this is:

routes.MapPageRoute("cgi-bin","cgi-bin/{* theRestcgi-bin}",〜/home/Search.aspx");

routes.MapPageRoute("cgi-bin", "cgi-bin/{*theRestcgi-bin}", "~/home/Search.aspx");

这就是查询字符串的神秘cgi-bin部分的来源,给人的印象是该框架实际上正在搜索某个虚拟/物理文件夹.

That's where that arcane cgi-bin part of the query string comes from, giving the impression that the framework is actually searching for some virtual/physical folder.

至于正确的解决方案,请按照您的方式定义合适的路由或将url指定为简单字符串.我认为,您的解决方案是更好的解决方案,因为您不必在RouteConfig类中的路由定义周围移动.

As for the proper solution: either define a suitable route or specify the url as a simple string the way you have done. I think, your solution is the better one as you won't have to move around the route definitions in the RouteConfig class.

这篇关于@ Url.Action("Action","Controller")返回"/cgi-bin?action = Action&amp; ampler = controller = Controller".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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