Consume Web API Post method with multiple parameter in ASP.NET MVC
In the previous article, we learned how to Create Web API post and Get method and display records in the ASP.NET View. Here, we will see how to consume Post method of Web API with multiple parameter.
Web API doesn't allow you to pass multiple complex objects in the method signature of a Web API controller method -- you can post only a single value to a Web API action method. This value in turn can even be a complex object. It is possible to pass multiple values though on a POST or a PUT operation by mapping one parameter to the actual content and the remaining ones via query strings.
Example :
[HttpGet]
public ActionResult Product()
{
string responseMessage = "";
DAL.AuthRequest at = new DAL.AuthRequest();
at.AuthToken = "2hgh343bjb";
at.Code = "8291343435443";
at.UserAgent = "";
DAL.ProductService hm = new DAL.ProductService();
hm.AuthRequest = at;
hm.ProductName = 1;
System.Net.ServicePointManager.Expect100Continue = false;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.example.com/product/query");
request.Method = "POST";
request.ContentType = "application/json";
string jsonOrder = JsonConvert.SerializeObject(hm);
var data = Encoding.UTF8.GetBytes(jsonOrder);
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
if (((HttpWebResponse)response).StatusDescription == "OK")
{
using (var stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
responseMessage = reader.ReadToEnd();
}
}
return View(hm);
}
public ActionResult Product()
{
string responseMessage = "";
DAL.AuthRequest at = new DAL.AuthRequest();
at.AuthToken = "2hgh343bjb";
at.Code = "8291343435443";
at.UserAgent = "";
DAL.ProductService hm = new DAL.ProductService();
hm.AuthRequest = at;
hm.ProductName = 1;
System.Net.ServicePointManager.Expect100Continue = false;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.example.com/product/query");
request.Method = "POST";
request.ContentType = "application/json";
string jsonOrder = JsonConvert.SerializeObject(hm);
var data = Encoding.UTF8.GetBytes(jsonOrder);
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
if (((HttpWebResponse)response).StatusDescription == "OK")
{
using (var stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
responseMessage = reader.ReadToEnd();
}
}
return View(hm);
}
DAL.cs :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace RestProductQuery.DataService
{
public class DAL
{
public class AuthRequest
{
public string Code { get; set; }
public string AuthToken { get; set; }
public string UserAgent { get; set; }
}
public class ProductService
{
public AuthRequest AuthRequest { get; set; }
public string ProductName { get; set; }
public int ProductType { get; set; }
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace RestProductQuery.DataService
{
public class DAL
{
public class AuthRequest
{
public string Code { get; set; }
public string AuthToken { get; set; }
public string UserAgent { get; set; }
}
public class ProductService
{
public AuthRequest AuthRequest { get; set; }
public string ProductName { get; set; }
public int ProductType { get; set; }
}
}
}
Here using this Product method we have passed multiple parameter to web api with url : "https://www.example.com/product/query" and get list of matched record from web api methode.
See Other Tutorial :
* AngularJS CRUD Operation in ASP.Net MVC
* AngularJS With ASP.NET MVC
* Convert Rows to columns using 'Pivot' in SQL Server
* Mvc Registration page With user exist using Ajax method
* MVC 4 How to Perform Insert Update Delete Edit Select Operation
* MVC4 Edit,update,Delete,cancel inside gridview using sql database
* MVC 4 Gridview To Display Data Using SQL Server Database With Simple code
* Login page in asp.net Mvc4 Web application
* Mvc4 How to bind Dropdown List using Sql Database
* Gridview find control in asp.net
Sign up here with your email
ConversionConversion EmoticonEmoticon