ASP-NET Razor Pages Routing

Viewed 58

Working on a Razor Page project where I would like to user to search for an input then I'll reroute to search page with parameters.

I'm getting URL Home/Search/Input where Input = user input. How would I go about getting the URL to be Home/Search?input instead?

Home page:

<form method="post" id="form">
<div class="d-flex text-center" id="search">
    <div class="container">
        <div class="card">
            <div class="card-body p-4">
                <div class="row">
                    <div class="col-12">
                        <div class="input-group">
                            <div class="form-outline flex-fill">
                                <input type="search" id="search" class="form-control form-control-lg" name="search"/>
                                <label class="form-label" style="font-size: 14px;vertical-align: middle" for="search">Enter Input...</label>
                            </div>
                            <button class="btn btn-success" id="submit">
                                <i class="fas fa-search"></i>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

Home cshtml.cs

public IActionResult OnPost()
    {
        var search = Request.Form["search"];
        return RedirectToPage("Search", new
        {
            search
        });
    }

Search cshtml.cs

public void OnGet(string search)
    {
        this.Input = (from customer in this._context.database
                          select customer).Where(x => x.Search== search).ToList();
    }

Search page:

@page "{search}"
@model SearchModel
@using DB
@{
   ViewData["Title"] = "Test";
 }
 <table class="table">
    @foreach (var customer in Model.Input)
    {
        <tr>
            <td>@customer.Search</td>
        </tr>
    }
 </table>
1 Answers

Change @page "{search}" to @page.

Then your url should be /Home/Search?search=input, where search is the name of the argument of your OnGet and OnPost method

Related