[ASP.NET] Trying to understand the [HttpGet] and [HttpPost] tags
I understand that the tag is used to provide precedence to one action method over another where same URL is matched.
However, I notice that tagging a method with [Http..] invalidates access via normal <a href> links.
For example, this would NOT work
Whereas these two would work:
In this case, I am wondering if it is still good practice to leave methods untagged so that they can be called via <a href> buttons? Or could this pose any security issues?
14 Replies
it depends on what you need.
a + href = GET by default. In controller you mark it POST ==> Rejected
your form use POST method => work
you can read more about them here : https://www.w3schools.com/tags/ref_httpmethods.asp
So it's not possible to have duplicate methods, with different tags [HttpGet]/[HttpPost]/unlabelled?
Was thinking of routing to the same method but differentiating by tag, I assume this is not possible?
Also, why is it that just adding a button will not call the IActionResult even with a type="submit"?
only work in <form>. Without form button type submit is the same as other buttons
So basically adding tags like <asp-controller="Login" asp-action="AddLoginCookie"> to buttons do nothing, and they can only call IActionResults if they're embedded within a form with designated action like the following?
I'm just clarifying if I understood this correctly
Is there a list of which HTTP elements are automatically considered POSTs or GETs?
I don't remember asp tag helper. You can set something like a href attribute using controller action if I'm not wrong
you would use a+href or form. a+href = GET by default. you declare method in form. else use JS to make request.
they are enough for me. Later you will learn FrontEnd framework. and you will use JS for most of the time
but are there any HTML/CSS tags that are explicitly POSTs?
I don't know hmhm. I would use form if i want vanilla stuff.
btw CSS has nothing to do with request
oh, yeah, mbad
how about JS? which ones are POSTs/GETs by default
It depends. Vanilla JS Fetch use GET by default. Other framework libraries may separate GET/POST into different methods to be used
Forms can be get or post, links are always posts, everything can be anything with enough javascript abuse
"links are always posts" - I beg to differ. A plain link is normally a GET
I meant gets