简体   繁体   English

如何将查询字符串参数转换为看起来像 ASP.NET Core 的目录部分?

[英]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.

相关问题 如何在ASP.NET MVC 4中将查询字符串参数转换为路由 - How to convert query string parameters to route in asp.net mvc 4 如何在 asp.net 核心 1.0 c# 中将字符串(实际上是位图)转换为 IFormFile - How can I convert String (which is actually Bitmap) to IFormFile in asp.net core 1.0 c# 问:如何在ASP.NET Core中执行查询字符串? - Q: How can I execute a query string in ASP.NET Core? 如何将剩余的查询参数绑定到 ASP.NET Core 中的字典 - How to bind remaining query parameters to a dictionary in ASP.NET Core 我如何将一个字符串(json)转换为另一个字符串 - how can I convert a string (json) to look like another string 如何在共享服务器上托管的 ASP.Net Core 3.1 应用程序中存储生产机密,如连接字符串 - How can I store production secrets like connection string in ASP.Net Core 3.1 application, hosted on a shared server 如何将查询字符串重写为asp.net中的域? - how can i rewrite query string to domains in asp.net? 如何在 ASP.NET Core MVC 中使用 ADO.NET 将参数添加到存储过程? - How can I add parameters to a stored procedure using ADO.NET in ASP.NET Core MVC? 如何构建一个 ASP.Net 核心 web api Enpoint 处理带有等号“=”符号的查询参数 - How do I build an ASP.Net core web api Enpoint that handles query parameters with an equal '=' sign 如何在 ASP.NET Core 3.0 中使用 ValidationAttribute 验证多个操作参数 - How can I validate multiple action parameters with ValidationAttribute in ASP.NET Core 3.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM