简体   繁体   English

T4MVC:如何使用说Url.Action?

[英]T4MVC: How to use with say Url.Action?

T4MVC documentation hints we can write 我们可以编写T4MVC文档提示

   @Url.Action(MVC.Dinners.Delete(Model.DinnerID))

indstead of the old fashioned and string hardcoded 代表老式和字符串硬编码

   @Url.Action("Delete", "Dinners", ...))

(see attached pic) (见附图)

在此输入图像描述

Maybe I am seriously missing something, but the first version will actually call the controller's action method, what obviously not the goal + have side effects. 也许我严重缺少一些东西,但第一个版本实际上会调用控制器的动作方法,显然不是目标+有副作用。 I've followed the method call statically with resharper, and also debugged: The method was actually called, which (again) is not the goal. 我已经使用resharper静态跟踪方法调用,并且还调试了:实际上调用了该方法,其中(再次)不是目标。

It's true that Url.Action have overload which accepts ActionResult that's why the T4MVC version does not cause compile error. Url.Action确实具有接受ActionResult的重载,这就是为什么T4MVC版本不会导致编译错误的原因。 It's also true in case the action method can be called without side effect the url rendered correctly. 如果可以在没有副作用的情况下调用操作方法,则正确呈现的URL也是如此。

My goal is to render the action url in the view, with other words I am really seeking the alternative of @Url.Action("Delete", "Dinners", ...)) . 我的目标是在视图中渲染动作URL,换句话说,我真的在寻找@Url.Action("Delete", "Dinners", ...))的替代方案。

Here is what I found: 这是我发现的:

 @Url.Action(MVC.Dinner.ActionNames.Delete, MVC.Dinner.Name)

What am I missing? 我错过了什么? (and why would we call the actual action method, just to render its url?) (为什么我们会调用实际的动作方法,只是为了渲染它的url?)

That was fatal. 那是致命的。 All true what I described in the question, still it is false alert. 我在问题中所描述的一切都是真的,仍然是虚假警报。

My action method's return value was string . 我的action方法的返回值是string Please do not ask why, it is completely legal, and logically that method is void (called as POST from javascript, and response is ignored) 请不要问为什么,它是完全合法的,逻辑上该方法是无效的(从javascript调用POST,并忽略响应)

That string return value fooled the T4MVC generator, so the method was actually called by the @Url.Action(MVC.Dinners.Delete(Model.DinnerID)) 该字符串返回值欺骗了T4MVC生成器,因此该方法实际上是由@Url.Action(MVC.Dinners.Delete(Model.DinnerID))

After changing the method's return value to ActionResult (and doing nothing else) the T4MVC generated code is changed and the action method was not called anymore 将方法的返回值更改为ActionResult(并且不执行任何其他操作)后,T4MVC生成的代码将更改,并且不再调用操作方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM