Sourav Kumar Das

Sourav Kumar Das

  • 525
  • 2.5k
  • 171.3k

WEB API code how to consume it in ASP.Net MVC

Dec 31 2019 4:52 AM
Hi All,
 
I have a question that I have a post method code in WEB API in which I need to consume it in MVC.
 
  1. [HttpPost]   
  2.        [AllowAnonymous]   
  3.        [Route("NewVendorRegistration")]   
  4.        public IHttpActionResult InsertVendor()   
  5.        {   
  6.            //Passing Values in form data in PostMan   
  7.    
  8.            var FirstName = HttpContext.Current.Request.Params["FirstName"];   
  9.            var LastName = HttpContext.Current.Request.Params["LastName"];   
  10.            var EmailId = HttpContext.Current.Request.Params["EmailId"];   
  11.            var MobileNo = HttpContext.Current.Request.Params["MobileNo"];   
  12.            var UserName = HttpContext.Current.Request.Params["UserName"];   
  13.            var Password = HttpContext.Current.Request.Params["Password"];   
  14.            var Address = HttpContext.Current.Request.Params["Address"];   
  15.            var IDProof = HttpContext.Current.Request.Params["IDProof"];   
  16.            var VendorPhoto = HttpContext.Current.Request.Files["VendorPhoto"];   
  17.            var CreatedBy = HttpContext.Current.Request.Params["CreatedBy"];   
  18.    
  19.            if (FirstName != null && LastName != null && EmailId != null && MobileNo != null && UserName != null   
  20.                && Password != null && Address != null && IDProof !=null && CreatedBy != null)   
  21.            {   
  22.    
  23.                string sReturn = string.Empty;   
  24.                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);   
  25.                SqlCommand cmd = new SqlCommand("spVendorRegistration", con);   
  26.                cmd.CommandType = CommandType.StoredProcedure;   
  27.    
  28.                cmd.Parameters.AddWithValue("@QueryType""Insert");   
  29.                cmd.Parameters.AddWithValue("@FirstName", FirstName.Trim());   
  30.                cmd.Parameters.AddWithValue("@LastName", LastName.Trim());   
  31.                cmd.Parameters.AddWithValue("@EmailId", EmailId.Trim());   
  32.                cmd.Parameters.AddWithValue("@MobileNo", MobileNo.Trim());   
  33.                cmd.Parameters.AddWithValue("@Address", Address.Trim());   
  34.                cmd.Parameters.AddWithValue("@IDProof", IDProof.Trim());   
  35.                cmd.Parameters.AddWithValue("@UserName",UserName.Trim());   
  36.                cmd.Parameters.AddWithValue("@Password", Password.Trim());   
  37.                cmd.Parameters.AddWithValue("@ActiveStatus""Y");   
  38.                cmd.Parameters.AddWithValue("@CreatedBy", CreatedBy.Trim());   
  39.                if (VendorPhoto != null)   
  40.                {   
  41.                    IList AllowedFileExtensions = new List { ".jpg"".jpeg"".png" };   
  42.                    var ext = VendorPhoto.FileName.Substring(VendorPhoto.FileName.LastIndexOf('.'));   
  43.                    var extension = ext.ToLower();   
  44.                    if (!AllowedFileExtensions.Contains(extension))   
  45.                    {   
  46.                        VendorStr VendorData = new VendorStr   
  47.                        {   
  48.                            Response = "Please Upload image of type .jpg,.png.",   
  49.                            StatusCode = 0   
  50.                        };   
  51.                        return Ok(VendorData);   
  52.                    }   
  53.    
  54.                    int MaxContentLength = 1024 * 1024 * 1; //Size = 1 MB     
  55.    
  56.                    if (VendorPhoto.ContentLength > MaxContentLength)   
  57.                    {   
  58.                        var message = string.Format("Please Upload a file upto 1 mb.");   
  59.    
  60.                        VendorStr VendorData = new VendorStr   
  61.                        {   
  62.                            Response = "Please Upload a file upto 1 mb.",   
  63.                            StatusCode = 0   
  64.                        };   
  65.                        return Ok(VendorData);   
  66.                    }   
  67.    
  68.                    var filePath = HttpContext.Current.Server.MapPath("~/Images/Vendor/" + VendorPhoto.FileName);   
  69.    
  70.                    if (File.Exists(filePath))   
  71.                    {   
  72.                        File.Delete(filePath);   
  73.                    }   
  74.                    VendorPhoto.SaveAs(filePath);   
  75.                    cmd.Parameters.AddWithValue("@VendorPhoto", filePath.Trim());   
  76.                }   
  77.                
  78.    
  79.                SqlParameter RuturnValue = new SqlParameter("@SQLReturn", SqlDbType.VarChar, 50);   
  80.                RuturnValue.Direction = ParameterDirection.Output;   
  81.                cmd.Parameters.Add(RuturnValue);   
  82.                con.Open();   
  83.                cmd.ExecuteNonQuery();   
  84.                sReturn = cmd.Parameters["@SQLReturn"].Value.ToString();   
  85.                string[] sResult = sReturn.Split('~');   
  86.                con.Close();   
  87.    
  88.                if (sResult[0].Trim() == "Success")   
  89.                {   
  90.                    VendorStr Vendors = new VendorStr   
  91.                    {   
  92.                        Response = "Successfully Inserted",   
  93.                        StatusCode = 1   
  94.                    };   
  95.                    return Ok(Vendors);   
  96.                }   
  97.                else if (sResult[0].Trim() == "Already")   
  98.                {   
  99.                    VendorStr Vendors = new VendorStr   
  100.                    {   
  101.                        Response = sResult[1].Trim(),   
  102.                        StatusCode = 0   
  103.                    };   
  104.                    return Ok(Vendors);   
  105.                }   
  106.                else   
  107.                {   
  108.                    VendorStr Vendors = new VendorStr   
  109.                    {   
  110.                        Response = "Must Pass Correct Input Parameters",   
  111.                        StatusCode = 0   
  112.                    };   
  113.                    return Ok(Vendors);   
  114.                }   
  115.            }   
  116.            else   
  117.            {   
  118.                VendorStr Vendors = new VendorStr   
  119.                {   
  120.                    Response = "Must Pass All Parameters",   
  121.                    StatusCode = 0   
  122.                };   
  123.                return Ok(Vendors);   
  124.            }   
  125.        }  

 
How I can consume this API Post method code in MVC and this code is in form data.
 
so if there's a solution please let me know it as soon as possible.

Answers (5)