TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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.
[HttpPost]
[AllowAnonymous]
[Route(
"NewVendorRegistration"
)]
public
IHttpActionResult InsertVendor()
{
//Passing Values in form data in PostMan
var FirstName = HttpContext.Current.Request.Params[
"FirstName"
];
var LastName = HttpContext.Current.Request.Params[
"LastName"
];
var EmailId = HttpContext.Current.Request.Params[
"EmailId"
];
var MobileNo = HttpContext.Current.Request.Params[
"MobileNo"
];
var UserName = HttpContext.Current.Request.Params[
"UserName"
];
var Password = HttpContext.Current.Request.Params[
"Password"
];
var Address = HttpContext.Current.Request.Params[
"Address"
];
var IDProof = HttpContext.Current.Request.Params[
"IDProof"
];
var VendorPhoto = HttpContext.Current.Request.Files[
"VendorPhoto"
];
var CreatedBy = HttpContext.Current.Request.Params[
"CreatedBy"
];
if
(FirstName != null && LastName != null && EmailId != null && MobileNo != null && UserName != null
&& Password != null && Address != null && IDProof !=null && CreatedBy != null)
{
string sReturn = string.Empty;
SqlConnection con =
new
SqlConnection(ConfigurationManager.ConnectionStrings[
"ConnStr"
].ConnectionString);
SqlCommand cmd =
new
SqlCommand(
"spVendorRegistration"
, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(
"@QueryType"
,
"Insert"
);
cmd.Parameters.AddWithValue(
"@FirstName"
, FirstName.Trim());
cmd.Parameters.AddWithValue(
"@LastName"
, LastName.Trim());
cmd.Parameters.AddWithValue(
"@EmailId"
, EmailId.Trim());
cmd.Parameters.AddWithValue(
"@MobileNo"
, MobileNo.Trim());
cmd.Parameters.AddWithValue(
"@Address"
, Address.Trim());
cmd.Parameters.AddWithValue(
"@IDProof"
, IDProof.Trim());
cmd.Parameters.AddWithValue(
"@UserName"
,UserName.Trim());
cmd.Parameters.AddWithValue(
"@Password"
, Password.Trim());
cmd.Parameters.AddWithValue(
"@ActiveStatus"
,
"Y"
);
cmd.Parameters.AddWithValue(
"@CreatedBy"
, CreatedBy.Trim());
if
(VendorPhoto != null)
{
IList AllowedFileExtensions =
new
List {
".jpg"
,
".jpeg"
,
".png"
};
var ext = VendorPhoto.FileName.Substring(VendorPhoto.FileName.LastIndexOf(
'.'
));
var extension = ext.ToLower();
if
(!AllowedFileExtensions.Contains(extension))
{
VendorStr VendorData =
new
VendorStr
{
Response =
"Please Upload image of type .jpg,.png."
,
StatusCode = 0
};
return
Ok(VendorData);
}
int
MaxContentLength = 1024 * 1024 * 1;
//Size = 1 MB
if
(VendorPhoto.ContentLength > MaxContentLength)
{
var message = string.Format(
"Please Upload a file upto 1 mb."
);
VendorStr VendorData =
new
VendorStr
{
Response =
"Please Upload a file upto 1 mb."
,
StatusCode = 0
};
return
Ok(VendorData);
}
var filePath = HttpContext.Current.Server.MapPath(
"~/Images/Vendor/"
+ VendorPhoto.FileName);
if
(File.Exists(filePath))
{
File.Delete(filePath);
}
VendorPhoto.SaveAs(filePath);
cmd.Parameters.AddWithValue(
"@VendorPhoto"
, filePath.Trim());
}
SqlParameter RuturnValue =
new
SqlParameter(
"@SQLReturn"
, SqlDbType.VarChar, 50);
RuturnValue.Direction = ParameterDirection.Output;
cmd.Parameters.Add(RuturnValue);
con.Open();
cmd.ExecuteNonQuery();
sReturn = cmd.Parameters[
"@SQLReturn"
].Value.ToString();
string[] sResult = sReturn.Split(
'~'
);
con.Close();
if
(sResult[0].Trim() ==
"Success"
)
{
VendorStr Vendors =
new
VendorStr
{
Response =
"Successfully Inserted"
,
StatusCode = 1
};
return
Ok(Vendors);
}
else
if
(sResult[0].Trim() ==
"Already"
)
{
VendorStr Vendors =
new
VendorStr
{
Response = sResult[1].Trim(),
StatusCode = 0
};
return
Ok(Vendors);
}
else
{
VendorStr Vendors =
new
VendorStr
{
Response =
"Must Pass Correct Input Parameters"
,
StatusCode = 0
};
return
Ok(Vendors);
}
}
else
{
VendorStr Vendors =
new
VendorStr
{
Response =
"Must Pass All Parameters"
,
StatusCode = 0
};
return
Ok(Vendors);
}
}
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.
Reply
Answers (
5
)
WEB API consuming in MVC is throwing an error?
How to Display embedded base64 image in Excel