Perform CURD Web API Service Call Function In .NET Core 2.0 And 2.1 MVC (CSHTML)

Introduction

 
API Web service call functions in .NET Core MVC are based on CSHTML. It runs on multiple operating systems because of its cross-platform support. CSHTML generates the client web browser.
 
The below service model name is WebApiCallCoreMVC.Models and the controller name is MobileuserController.cs.
 
This service based on System.Net.Http. It is formatting-oriented and easy to handle on the data Grid View layout.
 
Step 1
 
Create a new project.
 
Perform CURD Web API Service Call Function In .Net Core 2.0 And 2.1 MVC (CSHTML)
 
Step 2
 
Choose Web and Select  ASP.NET Web Application (.NET Framework)
 
Perform CURD Web API Service Call Function In .Net Core 2.0 And 2.1 MVC (CSHTML)
 
Choose the MVC
 
Perform CURD Web API Service Call Function In .Net Core 2.0 And 2.1 MVC (CSHTML)
 
Add on DLL Reference (System.Net.Http.Formatting, Newtonsoft.json)
 
Perform CURD Web API Service Call Function In .Net Core 2.0 And 2.1 MVC (CSHTML)
 
Step 3
 
Add class Users.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5.   
  6. namespace WebApiCallCoreMVC.Models  
  7. {  
  8.     public class Users  
  9.     {  
  10.         public int ID { getset; }  
  11.         public string Username { getset; }  
  12.         public string Mobile { getset; }  
  13.     }  
  14. }  
Add Controller class MobileuserController.cs
 
Create Index
  1. // GET: Mobileuser  
  2.         public ActionResult Index()  
  3.         {  
  4.             IEnumerable<Users> emp = null;  
  5.   
  6.             using (var client = new HttpClient())  
  7.             {  
  8.                 client.BaseAddress = new Uri("http://localhost:2514/");  
  9.                 //HTTP GET  
  10.                 var responseTask = client.GetAsync("api/values");  
  11.                 responseTask.Wait();  
  12.   
  13.                 var result = responseTask.Result;  
  14.                 if (result.IsSuccessStatusCode)  
  15.                 {  
  16.                     var readTask = result.Content.ReadAsAsync<IList<Users>>();  
  17.                     readTask.Wait();  
  18.   
  19.                     emp = readTask.Result;  
  20.                 }  
  21.                 else //web api sent error response   
  22.                 {  
  23.                     //log response status here..  
  24.   
  25.                     emp = Enumerable.Empty<Users>();  
  26.   
  27.                     ModelState.AddModelError(string.Empty, "Server error. Please contact administrator.");  
  28.                 }  
  29.             }  
  30.             return View(emp);  
  31.         }  
Perform CURD Web API Service Call Function In .Net Core 2.0 And 2.1 MVC (CSHTML)
  1. @model IEnumerable<WebApiCallCoreMVC.Models.Users>  
  2.   
  3. @{  
  4.     ViewData["Title"] = "Index";  
  5.     Layout = "~/Views/Shared/_Layout.cshtml";  
  6. }  
  7.   
  8. <h2>Index</h2>  
  9.   
  10. <p>  
  11.     <a asp-action="Create">Create New</a>  
  12. </p>  
  13. <table class="table">  
  14.     <thead>  
  15.         <tr>  
  16.             <th>  
  17.                 @Html.DisplayNameFor(model => model.ID)  
  18.             </th>  
  19.             <th>  
  20.                 @Html.DisplayNameFor(model => model.Username)  
  21.             </th>  
  22.             <th>  
  23.                 @Html.DisplayNameFor(model => model.Mobile)  
  24.             </th>  
  25.             <th></th>  
  26.         </tr>  
  27.     </thead>  
  28.     <tbody>  
  29. @foreach (var item in Model) {  
  30.         <tr>  
  31.             <td>  
  32.                 @Html.DisplayFor(modelItem => item.ID)  
  33.             </td>  
  34.             <td>  
  35.                 @Html.DisplayFor(modelItem => item.Username)  
  36.             </td>  
  37.             <td>  
  38.                 @Html.DisplayFor(modelItem => item.Mobile)  
  39.             </td>  
  40.             <td>  
  41.                 @Html.ActionLink("Edit""Edit"new {  id=item.ID}) |  
  42.                 @*@Html.ActionLink("Details""Details"new { /* id=item.PrimaryKey */ })*@ |  
  43.                 @Html.ActionLink("Delete""Delete"new { id=item.ID })  
  44.             </td>  
  45.         </tr>  
  46. }  
  47.     </tbody>  
  48. </table>  
Perform CURD Web API Service Call Function In .Net Core 2.0 And 2.1 MVC (CSHTML)
 
Create Edit
  1. public ActionResult GetUpdate(Users us)  
  2.       {  
  3.           if (ModelState.IsValid)  
  4.           {  
  5.               string id = us.ID.ToString();  
  6.               string uname = us.Username;  
  7.               string MobNo = us.Mobile;  
  8.   
  9.               Users objProduct = new Users();  
  10.               objProduct.Username = uname;  
  11.               objProduct.Mobile = MobNo;  
  12.               string json = JsonConvert.SerializeObject(objProduct);  
  13.   
  14.               var baseAddress = "http://localhost:2514/api/values/GetMobileUpdate?ID=" + id + "&Username=" + uname + "&Mobile=" + MobNo + "";  
  15.   
  16.               var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));  
  17.               http.Accept = "application/json";  
  18.               http.ContentType = "application/json";  
  19.               http.Method = "PUT";  
  20.   
  21.               string parsedContent = json;  
  22.               ASCIIEncoding encoding = new ASCIIEncoding();  
  23.               Byte[] bytes = encoding.GetBytes(parsedContent);  
  24.   
  25.               Stream newStream = http.GetRequestStream();  
  26.               newStream.Write(bytes, 0, bytes.Length);  
  27.               newStream.Close();  
  28.   
  29.               var response = http.GetResponse();  
  30.   
  31.               var stream = response.GetResponseStream();  
  32.   
  33.           }  
  34.           return RedirectToAction("Index");  
  35.       }  
Perform CURD Web API Service Call Function In .Net Core 2.0 And 2.1 MVC (CSHTML)
  1. @model WebApiCallCoreMVC.Models.Users  
  2.   
  3. @{  
  4.     ViewData["Title"] = "Edit";  
  5.     Layout = "~/Views/Shared/_Layout.cshtml";  
  6. }  
  7.   
  8. <h2>Edit</h2>  
  9.   
  10. <h4>Users</h4>  
  11. <hr />  
  12. @using (Html.BeginForm("GetUpdate""Mobileuser", FormMethod.Post))  
  13. {  
  14.     <div class="row">  
  15.         <div class="col-md-4">  
  16.             <form asp-action="Edit">  
  17.                 <div asp-validation-summary="ModelOnly" class="text-danger"></div>  
  18.                 <div class="form-group">  
  19.                     <label asp-for="ID" class="control-label"></label>  
  20.                     <input asp-for="ID" class="form-control" />  
  21.                     <span asp-validation-for="ID" class="text-danger"></span>  
  22.                 </div>  
  23.                 <div class="form-group">  
  24.                     <label asp-for="Username" class="control-label"></label>  
  25.                     <input asp-for="Username" class="form-control" />  
  26.                     <span asp-validation-for="Username" class="text-danger"></span>  
  27.                 </div>  
  28.                 <div class="form-group">  
  29.                     <label asp-for="Mobile" class="control-label"></label>  
  30.                     <input asp-for="Mobile" class="form-control" />  
  31.                     <span asp-validation-for="Mobile" class="text-danger"></span>  
  32.                 </div>  
  33.                 <div class="form-group">  
  34.                     <input type="submit" value="Save" class="btn btn-default" />  
  35.                 </div>  
  36.             </form>  
  37.         </div>  
  38.     </div>  
  39. }  
  40.     <div>  
  41.         <a asp-action="Index">Back to List</a>  
  42.     </div>  
  43.   
  44.     @section Scripts {  
  45.         @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}  
  46.     }  
Perform CURD Web API Service Call Function In .Net Core 2.0 And 2.1 MVC (CSHTML)
 Perform CURD Web API Service Call Function In .Net Core 2.0 And 2.1 MVC (CSHTML)
Create Delete
  1. public ActionResult GetDelete(Users us)  
  2.         {  
  3.             if (ModelState.IsValid)  
  4.             {  
  5.                 string id = us.ID.ToString();  
  6.                 string uname = us.Username;  
  7.                 string MobNo = us.Mobile;  
  8.   
  9.                 Users objProduct = new Users();  
  10.                 objProduct.Username = uname;  
  11.                 objProduct.Mobile = MobNo;  
  12.                 string json = JsonConvert.SerializeObject(objProduct);  
  13.   
  14.                 var baseAddress = "http://localhost:2514/api/values?ID=" + id + "";  
  15.   
  16.                 var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));  
  17.                 http.Accept = "application/json";  
  18.                 http.ContentType = "application/json";  
  19.                 http.Method = "DELETE";  
  20.   
  21.                 string parsedContent = json;  
  22.                 ASCIIEncoding encoding = new ASCIIEncoding();  
  23.                 Byte[] bytes = encoding.GetBytes(parsedContent);  
  24.   
  25.                 Stream newStream = http.GetRequestStream();  
  26.                 newStream.Write(bytes, 0, bytes.Length);  
  27.                 newStream.Close();  
  28.   
  29.                 var response = http.GetResponse();  
  30.   
  31.                 var stream = response.GetResponseStream();  
  32.             }  
  33.             return RedirectToAction("Index");  
  34.         }  
Perform CURD Web API Service Call Function In .Net Core 2.0 And 2.1 MVC (CSHTML)
  1. @model WebApiCallCoreMVC.Models.Users  
  2.   
  3. @{  
  4.     ViewData["Title"] = "Delete";  
  5.     Layout = "~/Views/Shared/_Layout.cshtml";  
  6. }  
  7.   
  8. <h2>Delete</h2>  
  9.   
  10. @using (Html.BeginForm("GetDelete""Mobileuser", FormMethod.Post))  
  11. {  
  12.     <div>  
  13.         <fieldset>  
  14.             <legend>Mobile Number Information</legend>  
  15.             <div>  
  16.                 @Html.LabelFor(model => model.ID)  
  17.                 @Html.TextBoxFor(model => model.ID)  
  18.             </div> <br />  
  19.             <br />  
  20.             <input id="Submit" type="submit" value="Delete" />  
  21.             <script>  
  22.                document.getElementById("Submit").click();  
  23.             </script>  
  24.             <br />  
  25.   
  26.   
  27.         </fieldset>  
  28.     </div>  
  29. }  
  30.   
  31. <h3>Are you sure you want to delete this?</h3>  
  32. @using (Html.BeginForm("GetDelete""Mobileuser", FormMethod.Post))  
  33. {  
  34.     <div>  
  35.         <h4>Users</h4>  
  36.         <hr />  
  37.         <dl class="dl-horizontal">  
  38.             <dt>  
  39.                 @Html.DisplayNameFor(model => model.ID)  
  40.             </dt>  
  41.             <dd>  
  42.                 @Html.DisplayFor(model => model.ID)  
  43.             </dd>  
  44.             <dt>  
  45.                 @Html.DisplayNameFor(model => model.Username)  
  46.             </dt>  
  47.             <dd>  
  48.                 @Html.DisplayFor(model => model.Username)  
  49.             </dd>  
  50.             <dt>  
  51.                 @Html.DisplayNameFor(model => model.Mobile)  
  52.             </dt>  
  53.             <dd>  
  54.                 @Html.DisplayFor(model => model.Mobile)  
  55.             </dd>  
  56.         </dl>  
  57.   
  58.         <form asp-action="Delete">  
  59.             <input type="submit" value="Delete" class="btn btn-default" /> |  
  60.             <a asp-action="Index">Back to List</a>  
  61.         </form>  
  62.     </div>  
  63. }  
Perform CURD Web API Service Call Function In .Net Core 2.0 And 2.1 MVC (CSHTML)
Create New
  1. @model WebApiCallCoreMVC.Models.Users  
  2.   
  3. @{  
  4.     ViewData["Title"] = "Create";  
  5.     Layout = "~/Views/Shared/_Layout.cshtml";  
  6. }  
  7.   
  8. <h2>Create</h2>  
  9.   
  10. <h4>Users</h4>  
  11. <hr />  
  12. @using (Html.BeginForm("getCreate""Mobileuser", FormMethod.Post))  
  13. {  
  14.     <div class="row">  
  15.         <div class="col-md-4">  
  16.             <form asp-action="Create">  
  17.                 <div asp-validation-summary="ModelOnly" class="text-danger"></div>  
  18.                 <div class="form-group">  
  19.                     @*<label asp-for="ID" class="control-label"></label>  
  20.                     <input asp-for="ID" class="form-control" />  
  21.                     <span asp-validation-for="ID" class="text-danger"></span>*@  
  22.                 </div>  
  23.                 <div class="form-group">  
  24.                     <label asp-for="Username" class="control-label"></label>  
  25.                     <input asp-for="Username" class="form-control" />  
  26.                     <span asp-validation-for="Username" class="text-danger"></span>  
  27.                 </div>  
  28.                 <div class="form-group">  
  29.                     <label asp-for="Mobile" class="control-label"></label>  
  30.                     <input asp-for="Mobile" class="form-control" />  
  31.                     <span asp-validation-for="Mobile" class="text-danger"></span>  
  32.                 </div>  
  33.                 <div class="form-group">  
  34.                     <input type="submit" value="Create" class="btn btn-default" />  
  35.                 </div>  
  36.             </form>  
  37.         </div>  
  38.     </div>  
  39. }  
  40.     <div>  
  41.         <a asp-action="Index">Back to List</a>  
  42.     </div>  
  43.   
  44.     @section Scripts {  
  45.         @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}  
  46.     }  
Perform CURD Web API Service Call Function In .Net Core 2.0 And 2.1 MVC (CSHTML)
 
Perform CURD Web API Service Call Function In .Net Core 2.0 And 2.1 MVC (CSHTML)
 

Summary

 
The function is service-oriented and Create, Index, Edit, Delete are of CSHTML design.
 
The single value controller returns the design view. CSHTML is the default view engine of MVC projects. Most web page files use in CSHTML.
 
I hope this method helps you to create a Web API Service Call function in .NET Core 2.0 and 2.1 MVC (CSHTML).