简体   繁体   English

ASP.NET MVC中的异步GET / POST和操作名称冲突

[英]Async GET/POST and action name conflicts in ASP.NET MVC

The recommended way to make an edit page for ASP.NET MVC is to have two methods on a controller called Edit: one GET action and one POST action, both sharing the same name but overloaded differently. 为ASP.NET MVC创建编辑页面的推荐方法是在名为Edit的控制器上有两个方法:一个GET操作和一个POST操作,两者共享相同的名称但重载不同。 Validation errors are shown on the POST action if the edit fails. 如果编辑失败,则在POST操作上显示验证错误。 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. 然后,即使用户不在POST,用户也可以共享或添加URL:URL返回时返回GET版本。

So far, so good. 到现在为止还挺好。 But then there's the ASP.NET async pattern on controllers . 但是在控制器上ASP.NET异步模式 You have EditAsync and EditCompleted. 您有EditAsync和EditCompleted。 On the two different EditCompleted methods, how do you tell the GET apart from the POST? 在两个不同的EditCompleted方法中,您如何告诉GET除了POST? If you rename the POST action, you lose the nice behavior discussed earlier. 如果重命名POST操作,则会丢失前面讨论的好行为。

Is there a nice way to get these two patterns to work together? 有没有一种很好的方法让这两种模式一起工作?

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. 通常,XyzAsync()方法为XyzCompleted()方法提供一些状态对象,告诉它正在执行哪个工作单元,因此XyzCompleted()方法可以检查此对象并执行正确的操作。 However, if you want to have a different Completed method for each verb, this is possible via the following: 但是,如果要为每个动词使用不同的Completed方法,可以通过以下方法实现:

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

public ActionResult EditGetCompleted() { }

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

public ActionResult EditPostCompleted() { }

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

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