60

In MVC 3, is it possible to determine if an action is a result of a POST or GET method? I know you can decorate the actions with [HttpPost] and [HttpGet] to fire a specific action if one of those happens. What I'd like to do is leave those attributes off and programmatically determine which one caused the action.

The reason is, because of the way my search page is architected, I'm storing the search model in TempData. The initial search causes a POST to the search results page, but the paging links are all just links to "/results/2" (for page 2). They examine TempData to see if the model is in there an use it if so.

This causes problems when someone uses the back button to go to the search form and resubmit it. It's still picking up the model in TempData instead of using the new search criteria. So if it's a POST (i.e. someone just submitted the search form), I want to clear out TempData first.

Scott
  • 13,735
  • 20
  • 94
  • 152

5 Answers5

113

The HttpMethod property on the HttpRequest object will get it for you. You can just use:

if (HttpContext.Current.Request.HttpMethod == "POST")
{
    // The action is a POST.
}

Or you can get the Request object straight off of the current controller. It's just a property.

Arseni Mourzenko
  • 50,338
  • 35
  • 112
  • 199
John Bledsoe
  • 17,142
  • 5
  • 42
  • 59
  • For what it's worth, I'm also partial to always using GET requests to search and just putting all my parameters in the query string. It makes bookmarking, paging and other issues much easier to deal with. – John Bledsoe May 06 '11 at 17:03
  • 1
    Thanks for the quick response. I would have done that, but the search form is really quite complex with lots of combo boxes (multiple selections, etc). It was actually quite easy to exceed the max querystring length. – Scott May 06 '11 at 17:15
  • 7
    if you want constants, System.Net.WebRequestMethods.Http collection can be used. – d.popov Mar 13 '18 at 11:07
  • Just watch out, cache related bugs can be sneaky when using GET. – MetaGuru May 16 '20 at 22:19
21

Its better to compare it with HttpMethod Property rather than a string. HttpMethod is available in following namespace:

using System.Net.Http;

if (HttpContext.Request.HttpMethod == HttpMethod.Post.Method)
 {
 // The action is a post
 }
Phil Nicholas
  • 3,681
  • 1
  • 19
  • 23
Ans Bilal
  • 987
  • 1
  • 10
  • 28
12

Starting From .Net Core 3, you can use HttpMethods.Is{Verb}, like this:

using Microsoft.AspNetCore.Http

HttpMethods.IsPost(context.Request.Method);
HttpMethods.IsPut(context.Request.Method);
HttpMethods.IsDelete(context.Request.Method);
HttpMethods.IsPatch(context.Request.Method);
HttpMethods.IsGet(context.Request.Method);

You can even go further and create your custom extension to check whether it is a read operation or a write operation, something like this:

public static bool IsWriteOperation(this HttpRequest request) =>
    HttpMethods.IsPost(request?.Method) ||
    HttpMethods.IsPut(request?.Method) ||
    HttpMethods.IsPatch(request?.Method) ||
    HttpMethods.IsDelete(request?.Method);
Facundo La Rocca
  • 3,786
  • 2
  • 25
  • 47
8

To detect this in ASP.NET Core:

if (Request.Method == "POST") {
    // The action is a POST
}
AlliterativeAlice
  • 11,841
  • 9
  • 52
  • 69
2

If you're like me and prefer not to use a string literal (.net core 2.2 middleware using DI):

public async Task InvokeAsync(HttpContext context)
{
    if (context.Request.Method.Equals(HttpMethods.Get, StringComparison.OrdinalIgnoreCase))
    {
        …
    }
}
Marc Levesque
  • 154
  • 1
  • 6