在ASP.NET MVC异步GET / POST和动作名称冲突 [英] Async GET/POST and action name conflicts in ASP.NET MVC

查看:115
本文介绍了在ASP.NET MVC异步GET / POST和动作名称冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使ASP.NET MVC的编辑页面推荐的方法是有一个所谓的控制器编辑两种方法:一种GET动作和一个POST操作,都共享相同的名称,但重载不同。验证错误,如果编辑失败的POST操作中。然后,用户可以共享或书签,即使它掀起了文章的网址:网址去在返回GET版本

The recommended way to make an edit page for ASP.NET MVC is to have two methods on a controller called one GET action and one POST action, both sharing the same name but overloaded differently. Validation errors are shown on the POST action if the edit fails. Then the user can share or bookmark the URL even if it's off of a POST: the URL goes to the GET version on the return.

到目前为止,一切都很好。但是,再有就是 ASP.NET异步模式的控制器的。你有EditAsync和EditCompleted。在两种不同的方法EditCompleted,你怎么分辨POST的GET分开?如果重命名POST操作,你失去了前面讨论过的很好的行为。

So far, so good. But then there's the ASP.NET async pattern on controllers. You have EditAsync and EditCompleted. On the two different EditCompleted methods, how do you tell the GET apart from the POST? If you rename the POST action, you lose the nice behavior discussed earlier.

有没有一个很好的办法让这两种模式一起工作?

Is there a nice way to get these two patterns to work together?

推荐答案

一般来说,XyzAsync()方法提供了XyzCompleted()方法的一些状态对象,告诉它正在执行什么单位工作,所以XyzCompleted()方法检查该对象和做正确的事。但是,如果你想有一个不同的已完成的每个动词的方法,这是有可能通过以下内容:

Generally the XyzAsync() method provides the XyzCompleted() method some state object that tells it what unit of work is being performed, so the XyzCompleted() method can inspect this object and do the right thing. However, if you want to have a different Completed method for each verb, this is possible via the following:

[ActionName("Edit"), HttpGet]
public void EditGetAsync() { }

public ActionResult EditGetCompleted() { }

[ActionName("Edit"), HttpPost]
public void EditPostAsync() { }

public ActionResult EditPostCompleted() { }

这篇关于在ASP.NET MVC异步GET / POST和动作名称冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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