Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从ASP.NET MVC向Web Api发送数据_C#_Asp.net_Asp.net Mvc_Asp.net Web Api - Fatal编程技术网

C# 从ASP.NET MVC向Web Api发送数据

C# 从ASP.NET MVC向Web Api发送数据,c#,asp.net,asp.net-mvc,asp.net-web-api,C#,Asp.net,Asp.net Mvc,Asp.net Web Api,在Web Api中,我有一个简单的方法,它采用modelStudent: // POST api/values [HttpPost] public void CreateStudent([FromBody]Student student) { db.Students.Add(student); db.SaveChanges(); } 我的MVC应用程序的方法: public void Form(string Name, string Surname, string Qualif

在Web Api中,我有一个简单的方法,它采用model
Student

// POST api/values
[HttpPost]
public void CreateStudent([FromBody]Student student)
{
    db.Students.Add(student);
    db.SaveChanges();
}
我的MVC应用程序的方法:

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 I must send student object to Web Api with ulr "http://localhost:2640/api/values"
}

我想把我的MVC应用程序对象Student发送到WebAPI,但我不知道怎么做。我必须做什么?

假设有这样一个Web API控制器:

public class StudentsController : ApiController {
    // POST api/students
    [HttpPost]
    public IHttpActionResult Post(Student student) {
        db.Students.Add(student);
        db.SaveChanges();
        return Ok();
    }
}
位于以下端点

http://localhost:2640/api/students
您可以使用
HttpClient
从MVC控制器与WebApi通信

[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 I must send student object to 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);
}
[HttpPost]
公共异步任务表单(字符串名称、字符串姓氏、字符串限定、字符串专业、双重评级){
学生=新生{
Name=Name,
姓,
资格=资格,
专业=专业,
评级=评级
};
//在这里,我必须将student对象发送到Web Api
//URL:“http://localhost:2640/api/students"
var client=新的HttpClient();
汽车终点=”http://localhost:2640/api/students";
var response=wait client.PostAsJsonAsync(端点,学生);
if(响应。IsSuccessStatusCode){
返回操作(“索引”);
}
返回视图(学生);
}

有很多关于MVC和WebAPI的教程。你试过什么?你能发布你的代码吗?你的Web API和你的MVC在同一个网站上吗?我发布了我的代码。我在努力,但我做不到。Web Api-1项目MVC应用程序-2项目不工作,我没有任何错误,但我检查了response.IsSuccessStatusCode及其错误什么不工作?您从答案中使用了什么?我使用了此方法,但未将数据保存到web api数据库。请确保URI与您的web api正确匹配。这只是一个例子。调试您的代码并逐步执行,以确保web api确实被调用。一切正常,非常感谢。此处出现错误var response=wait client.PostAsJsonAsync(“api/students”,student),必须是看起来像var response=wait client.PostAsJsonAsync(“,student”)的web api的全名;