HTTPPost Variations

Feb 21, 2013 at 5:15 PM
I noticed that for the Create method in the StoreManager Controller, the HTTPPost Action Method is also named Create (same as the Get method), but for the Delete method, the HTTPPost Action method is named DeleteConfirmed with an ActionName parameter set to "Delete" (which I assume is referencing the Get method). Is there a reason for this? When would you use which style? Any ideas?
Mar 30, 2013 at 12:10 AM
This due to how Method Overload works and how it is evaluated by de CLR.
public ActionResult Delete(Model id);

[HttpPost]
public ActionResult Delete(Model id);
The above is what you want to avoid. The basic rule is that when overloading, the implicit agreement is that the signatures of both need to be different from each other. For instance, by having different arguments:
public ActionResult Delete();

[HttpPost]
public ActionResult Delete(Model id);
Though, chances are that your functionality demands the same arguments. The workaround is to rename one of the methods, and annotate a custom action name. So in this case a POST request which is fired at app/controller/delete/ is actually properly interpreted by the CLR. ASP Scaffolding takes that into account and automatically appends an actionname annotation.
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(Model id)