I an trying to implement a search query string for my searchbar.
Following the Microsoft documentation: Adding search, it suggested using Html.BeginForm("action", "controller", FormMethod.Get) method in view when I got the error:
This page isn’t working
If the problem continues, contact the site owner.
HTTP ERROR 405
My search query string shows:
localhost:7185/Searchbar__RequestVerificationToken=CfDJ8GCygMwCAINPiWbEfcvWp986TX925YtT_ivBMr4CJ0Cj6g6BDdH6xua1gReYA37rr5ljwc_GCuVXkANiQOt6hWJVJpOLr308aqLyyO6ii9fpf6DIbDlkKxYtOHHrN-O5eOxj4ie-TU-C-uBX2CNp0x4&SearchString=a&__RequestVerificationToken=CfDJ8GCygMwCAINPiWbEfcvWp986TX925YtT_ivBMr4CJ0Cj6g6BDdH6xua1gReYA37rr5ljwc_GCuVXkANiQOt6hWJVJpOLr308aqLyyO6ii9fpf6DIbDlkKxYtOHHrN-O5eOxj4ie-TU-C-uBX2CNp0x4
I have used [ValidateAntiForgeryToken] in the controller:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Index(string searchString)
{
//Code
}
Along with @Html.AntiForgeryToken() in the view:
@using (Html.BeginForm("Index", "Searchbar", FormMethod.Get))
{
@Html.AntiForgeryToken()
<form class="d-flex w-50 ps-lg-5" role="search" asp-controller="Searchbar" asp-action="Index">
<div class="input-group">
<input class="form-control shadow-none" value="@TempData["searchString"]" type="search" placeholder="Search" id="text-suggestion" aria-label="Search" name="SearchString"
required oninvalid="this.setCustomValidity(' ')" />
<button class="btn btn-outline-success shadow-none" type="submit" id="search-button"><i class='bx bx-search'></i></button>
</div>
</form>
}
[HttpPost]but yourHtml.BeginFormhasFormMethod.Getwhich will render to<form method="get">. Spot the difference.@Html.BeginFormcall will render a<form>element, but then you have a<form>tag-helper as a child element - which is incorrect.[HttpPost]and[ValidateAntiForgeryToken]from my controller.<form>elements?