官术网_书友最值得收藏!

Query strings

A query string is data that's sent as part of the URL that is not suitable for use as part of the hierarchical structure of the URL (where / separates between different levels of the hierarchy). Typically, the query string is the part that comes after the question mark in the URL and uses the & sign to separate between the different values. For example, the GiveNTake application allows the user to search for products in a specific location, and to constraint the search only to products that have an image attached to them. This API is exposed through a URL in the following format:

/api/products/searchcategory/category/subcategory?location=XXX&imageonly=true

ASP.NET Core automatically matches any value in the query string to a method parameter with the same name (this value is case-insensitive); therefore, the method in the controller is implemented like this:

[HttpGet("searchcategory/{category}/{subcategory=all}/")]
public string[] SearchByProducts(string category,string subcategory, string location="all", bool imageOnly=false )
{
return new[]
{
$"Category: {category}, Subcategory: {subcategory}, Location: {location}, Only with Images: {imageOnly}"
};
}

Run the application and navigate to
http://localhost:[port]/api/products/searchcategory/furniture/kitchen?location=center&imageonly=true.

Your browser should show a result similar to this:

By default, simple types are automatically mapped from the request query string to method parameters with the same name. If needed, you can force ASP.NET Core to map a method parameter to a query string value with a different name by using the  [FromQuery("name")] attribute. The [FromQuery] attribute also allows you to force the binding to a complex type.
主站蜘蛛池模板: 邻水| 武乡县| 陆川县| 陆川县| 上杭县| 缙云县| 辽阳县| 红桥区| 建水县| 金堂县| 巢湖市| 东丽区| 禄丰县| 桃江县| 兴化市| 阿合奇县| 疏附县| 高安市| 壶关县| 大余县| 克拉玛依市| 定边县| 集贤县| 临澧县| 图木舒克市| 缙云县| 山阴县| 寻乌县| 长顺县| 龙里县| 邢台市| 汉源县| 前郭尔| 昔阳县| 新巴尔虎左旗| 梅州市| 灵武市| 五峰| 时尚| 六安市| 黄浦区|