Detect if action is a POST or GET method

C#asp.net Mvc

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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
 }

Solution 3 - C#

To detect this in ASP.NET Core:

if (Request.Method == "POST") {
    // The action is a POST
}

Solution 4 - C#

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);

Solution 5 - C#

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))
    {
        …
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionScottView Question on Stackoverflow
Solution 1 - C#John BledsoeView Answer on Stackoverflow
Solution 2 - C#Ans BilalView Answer on Stackoverflow
Solution 3 - C#AlliterativeAliceView Answer on Stackoverflow
Solution 4 - C#Facundo La RoccaView Answer on Stackoverflow
Solution 5 - C#Marc LevesqueView Answer on Stackoverflow