What is the equivalent of if ($ _ POST) in .Net (ASP.Net MVC)?

7

I'm coding a page in Visual Studio, in ASP.Net MVC, and I need to display a div only when there is a POST on this page.

This is a form that will make POST on the page itself. When there is POST , I'll send a commit confirmation message.

What I have looks something like this:

@using (Html.BeginForm())
{
    <fieldset>
        <legend class="hidden">Contato</legend>

        <div class="msg-success">
            <p>Sua mensagem foi enviada com sucesso.</p>
        </div>

        @Html.LabelFor(m => m.Name, "Nome*")
        @Html.TextBoxFor(m => m.Name, new { id = "Name" })

        @Html.LabelFor(m => m.Email, "E-mail*")
        @Html.TextBoxFor(m => m.Email, new { id = "Email" })


        <input type="submit" id="Send" name="Send" value="Enviar" />
    </fieldset>
}

I would like an equivalence to if($_POST) to display div :

<div class="msg-success">
    <h3>Obrigado!</h3>
    <p>Sua mensagem foi enviada com sucesso.</p>
    <p>Aguarde, que logo entraremos em contato.</p>
    <span class="close">X</span>
</div>
    
asked by anonymous 01.09.2014 / 18:32

2 answers

11

In ASP.Net MVC, there are the action tags that indicate whether they will receive requests of Type GET or POST . As for example this:

[HttpPost] // Pode-se utilizar também [HttpGet], para receber requisições do tipo GET
public ActionResult ActionTest(object user)
{
    // esse action receberá apenas requisições do tipo POST
    return View();
}

Or there's a solution that's more the way you seem to look (Mode PHP ), which looks like this:

public ActionResult ActionTest(object user)
{
    if (Request.RequestType == "POST")
    {
        //faça algo se for POST
    }
    return View();
}

Where you can check the type of the request, this is not the best way, the 1st is more indicated, but nothing prevents you from using it.

  

In the second form you can validate any type of request, for example: GET, POST, PUT, DELETE, HEAD, etc.

     

Note: I've mentioned that the 1st way shown is better because it is more used, simpler to understand when looking at the code, and is present in most of the available ASP.NET MVC materials, but the 2 ways can be used, choosing the one that best suits your needs.

    
01.09.2014 / 18:52
1

Complementing @Fanando's response, there are some other equivalents, but they do not necessarily prevent a form from being submitted via GET :

1. Request.Form

Request.Form envelopes any and all values from the request, regardless of whether it is POST or GET . It is the basis of Model Binder .

2. Request.InputStream

Contains the entire HTTP request. It can be read as follows:

var dadosHttp = new System.IO.StreamReader(Request.InputStream).ReadToEnd(); 

In time, there is still the Attribute AcceptVerbs " that you can set for the Action filter if Action accepts POST , GET , both, or even others such as PUT , DELETE , HEAD , PATCH , etc.:

[AcceptVerbs(HttpVerbs.Post)] // Apenas POST
public ActionResult ActionPost(object user)
{
    // esse action receberá apenas requisições do tipo POST
    return View();
}

[AcceptVerbs(HttpVerbs.Get)] // Apenas GET
public ActionResult ActionGet(object user)
{
    // esse action receberá apenas requisições do tipo GET
    return View();
}

[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)] // Ambos
public ActionResult ActionAmbos(object user)
{
    return View();
}
    
24.04.2015 / 18:16