Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get the correct query string parameters by ASP.NET Core

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you the relevant knowledge about how to get the correct query string parameters from ASP.NET Core. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

By default, ASP.NET Core's model binding scans sources and fetches data in the order indicated in the following list in the HTTP request in the form of key-value pairs:

Form field

Request text

Routing data

Query string parameters

Uploaded file

Therefore, no configuration is required to get the query string parameters in Action:

[HttpGet] public string Demo (int id, int price) {return $"id = {id}, price = {price}";}

However, there is a pitfall in using query string parameters, and the Demo method can run when no parameters are passed, because model binding assigns type default values to missing parameters:

Since 0 is also the correct int value, we cannot tell whether this is a passed parameter value or not.

Price = 0 can cause serious business problems.

How do we deal with it?

Solution nullable type

For nullable types, the default is null. So you can determine whether query string parameters have been passed simply by checking whether they are null:

[HttpGet] public string Demo (int id, int? Price) {if (price==null) throw new ArgumentNullException (nameof (price)); return $"id = {id}, price= {price}";}

[BindRequired] attribute

Although the above scheme can achieve the requirements, but there are many code changes.

Another, simpler scenario is to use binding to validate properties:

[HttpGet] public string Demo (int id, [BindRequired] int price) {return $"id = {id}, price = {price}";}

Compared with the original method, you can add an attribute to the execution. And Swagger can correctly recognize this attribute:

These are all the contents of the article "how to get the correct query string parameters for ASP.NET Core". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report