0

I am at learning phase of web API and I want to POST data into database from the view by using WebAPI. How to hit the WebAPI from MVC.

Here is Controller

[HttpPost]
public ActionResult PatientMedicines(DomainModels.Orders.Medicines medicine, string key, string username, string FullName, string Phone, string CNIC, string address, string Email, DateTime dateofbirth, string Gender, string PaymentMethod, string PostedPrescription)
{
    SessionUser user = Session["User"] as SessionUser;
    medicine.add_with_api( key,user.Username, FullName,  Phone,  CNIC, address, Email, dateofbirth, Gender, PaymentMethod, PostedPrescription);//
    return View(new ViewModels.Orders.Medicines(user.Username));
}

Now Here is the Model

string baseUrl = ServerConfig.server_path + "/api/Payment/AddMedicineOrder";
Dictionary<string,string> parameters = new Dictionary<string,string>();

parameters.Add("username",username);
parameters.Add("FullName", FullName);
parameters.Add("Phone",Phone);
parameters.Add("CNIC",CNIC);
parameters.Add("address",address);
parameters.Add("Email",Email);
parameters.Add("dateofbirth",dateofbirth.ToShortDateString());
parameters.Add("Gender",Gender);
parameters.Add("PaymentMethod",PaymentMethod);
parameters.Add("image","null");
var response =Common.ReadFromAPI<API(baseUrl,"Post",parameters);
return true;

How to hit this from by using POST method to insert data through api?

1
  • You should reference directly from Microsoft on how to implement this, utilize stack overflow once you understand how the creator designed this pattern. Go here: learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/… Commented May 10, 2018 at 18:56

1 Answer 1

0

You should not use your model to do it. You should create a class that will be responsible to call the API. There are a lot of examples here explaining how to do it:

How to call API in asp.net MVC5

how to call web api or other restful web services in c#

Making a simple C# API call

Here a simple example calling an api from the controller:

public class HomeController : Controller  
{  
    //Hosted web API REST Service base url  
    string Baseurl = "http://xxx.xxx.xx.x:xxxx/";      
    public async Task<ActionResult> Index()  
    {  
        List<Employee> EmpInfo = new List<Employee>();  

        using (var client = new HttpClient())  
        {  
            //Passing service base url  
            client.BaseAddress = new Uri(Baseurl);  

            client.DefaultRequestHeaders.Clear();  
            //Define request data format  
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  

            //Sending request to find web api REST service resource GetAllEmployees using HttpClient  
            HttpResponseMessage Res = await client.GetAsync("api/Employee/GetAllEmployees");  

            //Checking the response is successful or not which is sent using HttpClient  
            if (Res.IsSuccessStatusCode)  
            {  
                //Storing the response details recieved from web api   
                var EmpResponse = Res.Content.ReadAsStringAsync().Result;  

                //Deserializing the response recieved from web api and storing into the Employee list  
                EmpInfo = JsonConvert.DeserializeObject<List<Employee>>(EmpResponse);  

            }  
            //returning the employee list to view  
            return View(EmpInfo);  
        }  
     }  
}  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.