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
If you want to know if the request is sent by form,you can try to check
content-type
in headers:Good,
you can always use the snippet
context.Request.HasFormContentType
, to check if there is a form attached to the request.