c# - Send data to Web Api from ASP.NET MVC -


in web api have simple method takes model student:

// post api/values [httppost] public void createstudent([frombody]student student) {     db.students.add(student);     db.savechanges(); } 

method of mvc application:

public void form(string name, string surname, string qualification, string specialty, double rating) {     student student = new student     {         name = name,         surname = surname,         qualification = qualification,         specialty = specialty,         rating = rating     };      //here must send student object web api ulr "http://localhost:2640/api/values" } 

and want send mvc application object student web api, dont know how can it. must do?

assuming web api controller this:

public class studentscontroller : apicontroller {     // post api/students     [httppost]     public ihttpactionresult post(student student) {         db.students.add(student);         db.savechanges();         return ok();     } } 

located @ following endpoint

http://localhost:2640/api/students 

you can use httpclient communicate webapi mvc controller

[httppost] public async task<actionresult> form(string name, string surname, string qualification, string specialty, double rating) {      student student = new student {         name = name,         surname = surname,         qualification = qualification,         specialty = specialty,         rating = rating     };      // here must send student object web api     // url: "http://localhost:2640/api/students"     var client = new httpclient();     car endpoint = "http://localhost:2640/api/students";     var response = await client.postasjsonasync(endpoint, student);     if(response.issuccessstatuscode) {         return redirecttoaction("index");     }     return view(student); } 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -