简体   繁体   中英

Routing in ASP.NET CORE Razor Pages with search parameters

How do you handle routing in razor pages with a search string like this: https://localhost:5000/link?searchParam1=daniel&searchParam2=olu&Keyword=keyword. In Razor pages, you can adding the routing you want in at the top of the razor.chshtml file like so:

@page "/preferedroute"
@model Index
@{

}

How do I do this with the search string above. It has multiple parameters. How do I translate that to the preferred route so that other pages can access it. I have a form that submits to the page.

<form class="form-inline" id="form" action="preferedroute">
<input id="searchParam1 value="dan">
 <input id="searchParam2 value="olu">
 <input id="keyword value="keyword">
</form> 

When I submit this form to page, it submits like so:

https://localhost:5000/link?searchParam1=daniel&searchParam2=olu&Keyword=keyword

but I need it to submit like so: https://localhost:5000/link/searchParam1/daniel/searchParam2/olu/Keyword=keyword

Now the problem is that I have this form in the shared _layout.cshtml file. So I cannot use tag helpers(asp-for) in the form like so because it does not have a page model:

  <form class="form-inline" id="form" action="preferedroute">
    <input asp-for="searchParam1" value="dan" >
     <input asp-for="searchParam2 value="olu">
     <input asp-for="keyword value="keyword">
    </form> 

How do I accomplish the required result when the form cannot use tag helpers?

How do you handle routing in razor pages with a search string like this: https://localhost:5000/link?searchParam1=daniel&searchParam2=olu&Keyword=keyword

You can refer to the following code snippet to bind and handle search terms.

public class LinkModel : PageModel
{
    [BindProperty(SupportsGet = true)]
    public SearchTerms searchTerms { get; set; }
    public void OnGet()
    {
        var searchParams = searchTerms;

        //...
        //code logic here
        //...
    }
}

Class SearchTerms

public class SearchTerms
{
    public string searchParam1 { get; set; }
    public string searchParam2 { get; set; }
    public string keyword { get; set; }
}

Test Result

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM