简体   繁体   中英

square brackets before object c#

I was learning .NET MVC 5 and had a question about the square brackets when using ModelBinder.

[HttpPost]
        public ActionResult Submit([ModelBinder(typeof(CustomerBinder))] Customer cust)
        {
            return View("Customer", cust);
        }

I don't understand how the square brackets work on objects. how the data from the form is transferred to the CustomerBinder? and in general what is the flow of the code of the Submit action parameter.

thanks in advance :)

These are called attributes , and there is an msdn turotial explaining them here .

Attributes provide a powerful method of associating metadata, or declarative information, with code (assemblies, types, methods, properties, and so forth). After an attribute is associated with a program entity, the attribute can be queried at run time by using a technique called reflection. For more information, see Reflection

EDIT:

To specifically address how MVC uses attributes, I would recommend continuing your MVC tutorial as this is a longer answer. If you would like one that I've found, here's a good one .

Briefly,

  • The [HttpPost] attribute let's MVC know only to allow POST requests to come through to this method, and not other Http Verbs like PUT or GET.
  • The [ModelBinder(typeof(CustomerBinder))] attribute informs the controller as to the binder that you want it to use for the object that gets passed in.

In this particular code:

[HttpPost]
public ActionResult Submit([ModelBinder(typeof(CustomerBinder))] Customer cust)
{
    return View("Customer", cust);
}

These are the parts:

  • [HttpPost] is a method attribute. It modifies the method with additional meta-data or meta-functionality. In this case it restricts HTTP access to that action to only POST verbs.
  • [ModelBinder(typeof(CustomerBinder))] is another attribute, but this modifies the particular argument to the method instead of the method itself. In this case the Customer cust argument. The ModelBinder attribute allows you to explicitly specify a model binder to use, so you can provide custom model binders for particular actions.

Essentially, MVC (and WebAPI) examines the incoming form values in the HTTP request and makes its best effort (using default model binders) to apply those values to the method arguments. In the vast majority of cases, this works just fine. Sometimes, however, you may want to implement custom functionality for this. So you can write your own model binders, and there are varying ways to tell the framework to use them. This is one such way, which applies a specific model binder ( CustomerBinder ) to a specific argument of only this specific method.

square brackets just are attributes. httppost and httpe get just limit the action to request type.

you can se link below for mor info: https://msdn.microsoft.com/en-us/library/z0w1kczw%28v=vs.80%29.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM