Invoking WebAPI Controller Action With Multiple Parameters In CORS Enabled Web Application

Description

It is used to invoke WebAPI Controller action with one or more than one parameters from client side in CORS enabled Web Application.

Advantage

  1. It avoids the need of creating separate entities every time while posting data to WebAPI Controller action.
  2. Supports Nullable data type parameters as well.
  3. It is especially for CORS enabled web applications, because as per regular behavior, we can’t invoke WebAPI Controller action with one or more than one parameter in CORS Enabled web applications.
  4. It supports all primitive and complex data type parameters as well.

Note

To install WebAPI CORS MULTI PARAMS MODELBINDER , run the following command in the Package Manager console.

PM>Install-Package WebAPICORSMULTIPARAMSMODELBINDER

After successful installation of above NuGet package, observe references added in your project file, as shown in below Fig.1



Do the below changes to the Global.asax.cs file before Configuring WebApiConfig.Register.

In order to generate and bind marked parameters as specified in our WebAPI controller action method, we are going to add MultiPostParams attribute on it.

Below Fig.2 demonstrates this:



Now, let's see how to pass data with different parameters with comma separated format to WebAPI Controller Action having MultiPostParams attribute on it from client side (i.e.from CORS Enabled Web Application).The  below Fig.3 demonstrates this:



Fig.4 shows how to add MultiPostParams attribute to WebAPI Controller Action.

Once this attribute is added just above WebAPI Controller Action, it creates/generates and binds marked parameters as specified in our WebAPI Controller action method to a specified parameter obtained from a client side call to invoke our WebAPI Controller Action.

The below figure demonstrates this:



Recommended Free Ebook
Similar Articles