skip to Main Content

I have a handler in my ASP.NET Core Razor Page:

public IActionResult OnPostDoSomething()
{
    // How can I know that the data is sent in query string, or in form, or in headers?
}

This method is called from multiple places, and each calls it differently. One via a form, the other via an AJAX call and sends parameters in query string.

I know I can check if (Request.Query["key"].Count > 0) and based on this understand which method has been used.

But I wonder if ASP.NET Core Razor Pages has a built-in technology to let me know that a form
exists or not. For example something like if (Request.FormExists).

The reason I’m looking for a dynamic solution, is because I’m building a dynamic code and I don’t have access to the name of the parameters being sent.

Is there a way for that?

2

Answers


  1. But I wonder if ASP.NET Core Razor Pages has a built-in technology to let me know that a form exists or not.

    If you want to know if the request is sent by form,you can try to check content-type in headers:

    if (HttpContext.Request.Headers["content-type"].ToString().Equals("application/x-www-form-urlencoded")) {
                    //request is sent by a form
                }
    
    Login or Signup to reply.
  2. Good,
    you can always use the snippet context.Request.HasFormContentType, to check if there is a form attached to the request.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search