ASP.NET Detailed explanation of model binding operation in core


ASP.Net Core model binding is a very useful feature. Generally, our controller processes HTTP request data by providing a record key through routing data, and then the published form field provides a value for the model. This process is tedious and error prone. Model binding automates the process and reduces the probability of error. This article introduces some common operation guidelines in model binding.

[bindproperty], [bindproperties] properties. The first property is a public property used for the controller or pagemodel class, so that the model binding targets this property; the second property is owned by. Net 2.1 and above, so that the model binding targets all the public properties of this class.

Model binding for HTTP get requests. The model binding of HTTP get request is [bindproperty (name = “Ai_ User “, supportsget = true)], add the above model binding value. We just need to set supportsget = true to true

[bindrequired] property. Bindrequired model bindings are often used for binding required items. As shown below, the bindrequired property binding belongs to the model binding. This binding is based on

Bindnever property. This property is opposite to the one above. This property does not allow property binding. It is generally used in the model of primary key ID.

Fromquery property. The binding for this property is to get the value from the query string. We can get the corresponding value from a bound string, that is, bind data to this value through other values. It’s a great way.

Fromheader property. This property gets the value from the HTTP header. Through HTTP transmission, we can assign the value to the corresponding field. As shown below, get the language value of the HTTP header and return its value.

These are all related knowledge points. Thank you for your support for developepaer.