MVC:ActionLink的记住ID字段? [英] MVC: ActionLink Remembers ID Field?

查看:111
本文介绍了MVC:ActionLink的记住ID字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个后续到一个较早的计算器的问题(链接文本)。

如果您使用默认的路由定义,这与{ID}结束,那么如果你有一个ActionLink的,其目标是生成页面的ActionLink的是同样的方法,框架自动地包含在回调URL中的id,即使你没有提出要求。

例如,如果你从下面的URL显示页面:

  http://www.somedomain.com/AController/SameMethod/456

和页面文件CSHTML具有如下所示的ActionLink的:

  @ Html.ActionLink(一些文本,SameMethod,ARouteValueDictionary,SomeHtmlAttributes)

那么你是否没有列入ARouteValueDictionaryID,它会在生成的URL显示出来。

如果你打电话给在第一时间产生的页面同样的方法这只发生。如果回调到一个不同的方法在同一控制器上的{ID}字段不会被插入到生成的URL。

我不一定有与此一问题。不过我很好奇,为什么设计师采取这种方式。

仅供参考,我因为我无意中取决于它在我的网站设计发现了这个功能。我必须通过ID字段回服务器,带着一帮其他信息...只有我从来没有明确添加了ID信息的RouteValueDictionary。但是因为我的大部分的回调是对这一已经在我有反正包含的信息首先产生的页面相同的操作方法。

您可以想像我的惊讶,当一个新的组件 - 我肯定是基本相同到什么是已经工作 - 失败。但由于新的组件有不同的目标操作方法,神奇的走了。

编辑:

修改澄清,包括生成的URL中的{ID}字段作为后生成的页面首先调用同样的方法队伍的解释。


解决方案

  

...框架包括自动的在回调URL中的id,
  即使你没有提出要求。


我想preFER术语ambiently而不是自动地。你已经可以想到路线令牌的网址为环境你的HtmlHelper和UrlHelpers。


  

但我很好奇,为什么设计师采取这种方式。


考虑一个控制器,组合在一起,说5种动作。这些5可能有联系对方,但不是很多的集团外的链接。 Html.Action最简单的过载只需要2 ARGS:文本渲染,动作名称

这使得速记从行动围绕链接到这些意见中采取行动。因为都是同一个控制器上,而该控制器已经在当前的行动路径,MVC重用这个值,当你不指定辅助方法控制器名称。同样的行为扩展到{ID},或者您定义的任何其他途径令牌。

This is a follow-on to an earlier stackoverflow question (link text).

If you use the default routing definition, which ends with {id}, then if you have an ActionLink whose target is the same method as generated the page the ActionLink is on, the framework automagically includes the id in the callback url, even if you didn't request it.

For example, if you're displaying a page from the following URL:

http://www.somedomain.com/AController/SameMethod/456

and the page cshtml file has an ActionLink like the following:

@Html.ActionLink("some text", "SameMethod", ARouteValueDictionary, SomeHtmlAttributes)

then whether or not you have "id" included in ARouteValueDictionary, it will show up in the generated URL.

This only occurs if you call back to the same method that generated the page in the first place. If you call back to a different method on the same controller the {id} field does not get inserted into the generated URL.

I don't necessarily have a problem with this. But I am curious as to why the designers took this approach.

FYI, I discovered this feature because I'd inadvertently been depending on it in my website design. I have to pass the ID field back to the server, along with a bunch of other information...only I'd never explicitly added the ID information to the RouteValueDictionary. But because most of my callbacks were to the same action method that had generated the page in the first place I was having the information included anyway.

You can imagine my surprise when a new component -- which I was sure was "essentially identical" to what was already working -- failed. But because the new component had a different target action method, the magic went away.

Edit:

Modified the explanation to clarify that including the {id} field in the generated URL is contingent upon calling the same method as generated the page in the first place.

解决方案

...the framework automagically includes the id in the callback url, even if you didn't request it.

I would prefer the term "ambiently" over "automagically". You can think of route tokens already in the URL as "ambient" to your HtmlHelper and UrlHelpers.

But I am curious as to why the designers took this approach.

Consider a Controller that groups together, say 5 actions. Those 5 may have links to each other, but not a lot of links outside the group. The simplest overload of Html.Action takes only 2 args: the text to render, and the action name.

This makes shorthand for linking around from action to action within these views. Since they are all on the same controller, and that controller is already in the path for the current action, MVC reuses this value when you don't specify the controller name in the helper method. The same behavior extends to {id}, or any other route token you define.

这篇关于MVC:ActionLink的记住ID字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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