Processing mail requests in ASP.NET MVC

I recently started working with MVC, before that I used the "classic" ASP.NET.

After using Ruby on Rails (RoR), I wondered how to implement POST request handling in MVC, similar to how RoR works. In RoR, you use the Post method, so you only need one function to represent.

In ASP.NET MVC, I need to use 2 separate functions for GET and for Post , so I need to initialize the same data twice, and I don't like repeating something in my code.

How to check if a Post request is one method?

Update:

solution found: I have to use Request.HttpMethod.

Thanks!

+4
source share
5 answers

You need only separate methods for GET and POST, if their method signatures are different, there is no reason why one action method cannot process GET and POST methods.

If you need to know if it was GET or POST, you can check with Request.HttpMethod in your action, but I would recommend using a separate method decorated with the [AcceptVerbs (HttpVerbs.Post)] attribute, as suggested by other posters.

+5
source

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:

  // this will not compile because the method signatures are the same public ActionResult MyReport(DateRangeReportItem report) { // if there are no validation errors and the required report parameters are completed if (ModelState.IsValid && report.ParametersAreComplete) { // retrieve report data and populate it on the report model report.Result = GetReportData(report.CreateReportParameters()); } return View(report); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult MyReport(DateRangeReportItem report) { if (ModelState.IsValid && report.ParametersAreComplete) { // 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 }); } else { // there were validation errors, or the report parameters are not yet complete return View(report); } } 

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

+9
source

You are not testing ASP.NET MVC. You decorate your method with the [AcceptVerbs(HttpVerbs.Post)] attribute to indicate that the method applies only to the message and accept the model in the method used to process the message.

I would suggest doing a walkthrough for NerdDinner to learn more about the ASP.NET MVC framework.

+3
source

You can see the Request.HttpMethod property.

+1
source

The correct way to use it is to use ModelBinding during a submit request.

 [AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(EmployeeViewModel model) { //validate data, save employee, handle validation errors... } 

This way you do not have to enter data again.

0
source

Source: https://habr.com/ru/post/1302161/


All Articles