[英]How can I convert query string parameters to look like a directory parts with ASP.NET Core?
I have an application that is written using C#
on the top of the ASP.NET Core 3.1 framework.我有一个在 ASP.NET Core 3.1 框架之上使用
C#
编写的应用程序。
Typically, Http Get request generates a URL with parameters to look something like this通常,Http Get 请求会生成一个 URL,其参数看起来像这样
/for_sale/Virginia_Heights/WV?beds=3+&type=MULTI-FAMILY,SINGLE-FAMILY_HOME_type
/for_sale/Virginia_Heights/WV?beds=3+&type=MULTI-FAMILY,SINGLE-FAMILY_HOME_type
However, I want a way to convert the previous URL to look something like the following instead但是,我想要一种方法将以前的 URL 转换为如下所示
/for_sale/Virginia_Heights/WV/3+_beds/MULTI-FAMILY,SINGLE-FAMILY_HOME_type
/for_sale/Virginia_Heights/WV/3+_beds/MULTI-FAMILY,SINGLE-FAMILY_HOME_type
Trulia has done something like this on their website (ie, https://www.trulia.com/for_sale/Virginia_Heights,WV/3p_beds/MULTI-FAMILY,SINGLE-FAMILY_HOME_type/ ) Trulia 在他们的网站上做了类似的事情(即https://www.trulia.com/for_sale/Virginia_Heights,WV/3p_beds/MULTI-FAMILY,SINGLE-FAMILY_HOME_type/ )
If you go to the previous URL and change the filter, you'll notice that any filter you chose will add that filter to the URL as a directory without adding ?=...
query parameters.如果您将 go 更改为之前的 URL 并更改筛选器,您会注意到您选择的任何筛选器都会将该筛选器作为目录添加到 URL,而无需添加
?=...
查询参数。
I am guessing the URL generation and data fetching are happening using the AJAX
technique with the help of Javascript. Isn't it?我猜 URL 生成和数据获取是在 Javascript 的帮助下使用
AJAX
技术进行的。不是吗? Or, could there be a better way to handle the URL generation?或者,有没有更好的方法来处理 URL 代?
The part I am struggling with here is building a flexible route in ASP.NET Core that would handle multiple optional parameters.我在这里苦苦挣扎的部分是在 ASP.NET Core 中构建一个灵活的路由来处理多个可选参数。
Question How can I create a route in ASP.NET Core that would allow me to handle such a request with many optional parameters to look like the example above?问题如何在 ASP.NET Core 中创建一条路由,使我能够处理带有许多可选参数的此类请求,如上例所示?
You have to set the route like this:您必须像这样设置路线:
[Route("/for_sale/Virginia_Heights/WV/{beds}/{type}]
public IActionResult GetPropertyInfo(string beds, string type)
{
// .....
}
/for_sale/Virginia_Heights/WV of course is not part of the answer. /for_sale/Virginia_Heights/WV 当然不是答案的一部分。 It's whatever your domain and other url structure may be.
无论您的域和其他 url 结构是什么,它都是如此。
It can be done by writting the controller method like below.We can specify that the beds and type parameter is from the route using the attribute [FromRoute].这可以通过编写如下所示的 controller 方法来完成。我们可以使用属性 [FromRoute] 指定床和类型参数来自路线。
[Route("/for_sale/Virginia_Heights,WV/{beds}/{type}] public IActionResult GetPropertyInfo([FromRoute]string beds, [FromRoute]string type) { // do something } We can also specify other model into it using [FromBody] atrribute. [Route("/for_sale/Virginia_Heights,WV/{beds}/{type}] public IActionResult GetPropertyInfo([FromRoute]string beds, [FromRoute]string type,[FromBody]Home model) { // do something }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.