0

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>
}
5
  • 1
    Your action has [HttpPost] but your Html.BeginForm has FormMethod.Get which will render to <form method="get">. Spot the difference. Commented Dec 9, 2023 at 6:07
  • 1
    Additionally, your @Html.BeginForm call will render a <form> element, but then you have a <form> tag-helper as a child element - which is incorrect. Commented Dec 9, 2023 at 6:07
  • @Dai Thank you for pointing that out. My code has began working after removing [HttpPost] and [ValidateAntiForgeryToken] from my controller. Commented Dec 9, 2023 at 6:12
  • 1
    ...so you're still (incorrectly) rendering nested <form> elements? Commented Dec 9, 2023 at 6:14
  • @Dai you are correct! I was rendering my View incorrectly. Now I have fixed it, the search query string is working perfectly. Thank you very much. Commented Dec 9, 2023 at 6:20

1 Answer 1

0

This is my Solution


  • Remove [HttpPost] from controller action.
    • As using FormMethod.Get with Html.BeginForm will render to <form method="get">, you cannot use [HttpPost] in controller which renders to <form method="post">.
public async Task<IActionResult> Index(string searchString)
{
    //Code
}
  • Remove <form> element from View
    • As Html.BeginForm renders a <form> element, your existing <form> tag-helper will behave as a child element.
    • Even without following this step your page will render, but this will cause the search query string to behave weirdly.
@using (Html.BeginForm("Index", "Searchbar", FormMethod.Get))
{
    <div class="input-group d-flex ps-lg-5">
        <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>
}

Thank you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.