I came across this question wanting to know the same thing. Below is a detailed description of my situation and the solution you are using (which uses the other answers presented here). Initially, I tried to use two approaches to a separate method, but I had a problem when the signature methods of these methods became identical.
I have a page displaying report data. At the top of the page there is a form with some fields that allow the user to specify report parameters, such as start date, end date, etc.
I initially approached this by creating two separate methods for handling the Get and Post methods. The post method redirected the browser to the get method so that any parameters that were specified were added to the query string and that the browser did not ask the user for a dialog with a message that he was going to re-send the data that they entered if they were updated. Note: later I realized that I could accomplish this by setting the method attribute of my form element to "Get", but I think that ideally the controller should not know how the view is implemented, so in my opinion, it does not matter.
Since I developed these two methods, I ended up in a situation where the method signatures became identical. In addition, my code for these two methods became almost identical, so I decided to combine them into one method and just check the verb of the request so that I could do something a little different when the request is not βGetβ. Below is a distilled example of my two methods:
Why do I accept a model object as a parameter to the get method? The reason is because I wanted to use the validation logic already built into the model object. If someone goes to my page directly with all the parameters already specified in the query line, I want to continue and get the report data and display it on the page. However, if the parameters specified in the query string are invalid, I also want the validation errors to appear on the page. By placing the model object as a parameter, the MVC environment will automatically try to fill it and will fix any validation errors without any additional work on my part.
I used other answers for this question to create the RequestHttpVerb property in the base controller class in my project:
public HttpVerbs RequestHttpVerb { get { return (HttpVerbs)Enum.Parse(typeof(HttpVerbs), this.Request.HttpMethod, true); } }
So my consolidated method is as follows:
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)] public ActionResult MyReport(DateRangeReportItem report) { // check if there are any validation errors in the model // and whether all required report parameters have been completed if (ModelState.IsValid && report.ParametersAreComplete) { // this is unnecessary if the form method is set to "Get" // but within the controller I do not know for sure if that will be the case in the view if (HttpVerbs.Get != this.RequestHttpVerb) { // redirect to the same action so that if the user refreshes the browser it will submit a get request instead of a post request // this avoids the browser prompting the user with a dialog saying that their data will be resubmitted return RedirectToAction("MyReport", new { StartDate = report.StartDate, EndDate = report.EndDate }); } // there were no validation errors and all required report parameters are complete // retrieve report data and populate that data on the model report.Result = GetReportData(report.CreateReportParameters()); } // display the view with the report object // Any model state errors that occurred while populating the model will result in validation errors being displayed return View(report); }
This is my current solution to the problem. I would prefer not to check the Request.HttpMethod property to determine if I need to redirect, but I did not see another solution to my problem. It would be nice for me to keep two different Get and Post request processing methods, but an identical method signature prevented this. I would prefer to rename my message handler method to avoid a method signing conflict and use some mechanism to indicate the MVC environment that my renamed method should still handle the βMyReportβ action, but I don't know any such mechanism in the structure MVC