C#C
C#4y ago
surwren

[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

[HttpPost]
        public IActionResult DelLoginCookie()
        {
            Response.Cookies.Delete("sessionId");

            return Redirect(Request.Headers["Referer"].ToString());
        }

...

<a class="btn btn-primary btn-sm" asp-action="DelLoginCookie" asp-controller="Login">Logout</a>


Whereas these two would work:

        public IActionResult DelLoginCookie()
        {
            Response.Cookies.Delete("sessionId");

            return Redirect(Request.Headers["Referer"].ToString());
        }

...

<a class="btn btn-primary btn-sm" asp-action="DelLoginCookie" asp-controller="Login">Logout</a>


[HttpPost]
        public IActionResult DelLoginCookie()
        {
            Response.Cookies.Delete("sessionId");

            return Redirect(Request.Headers["Referer"].ToString());
        }

...
<form role="search" action="/Login/DelLoginCookie" method="POST">
        <button class="btn btn-dark" id="test2" type="submit">Logout</button>
    </form>


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?
Was this page helpful?