웹 서비스를 사용할 웹 서비스 및 응용 프로그램을 만들어야합니다. MVC 컨트롤러에서 Web API 사용
나는 웹 API 프로젝트를 생성하고 내가 거기이 방법이 있습니다namespace StudentListApi.Controllers
{
//[Authorize]
public class ValuesController : ApiController
{
StudentContext db = new StudentContext();
// GET api/values
public IEnumerable<Student> GetStudents()
{
return db.Students;
}
//...
}
}
내가 컨트롤러에이 방법을 사용합니다. 내 MVC 사이트는 학생들의 데이터를 필요로합니다. 그리고보기에서이 데이터를 취합니다
public class HomeController : Controller
{
private const string APP_PATH = "http://localhost:2640";
private Models.StudentListEntities db = new Models.StudentListEntities();
public ActionResult Index()
{
//var students = db.Students.OrderByDescending(x => x.Id);
using (var client = new HttpClient())
{
var students = client.GetAsync(APP_PATH + "/api/value").Result;
//return students.StatusCode.ToString();
return View(students);
}
}
}
을하지만 작동하지 않습니다 내가 오류가 있습니다
오류는 러시아어,하지만 난 당신의 이름에서 이해할 수 있다고 생각 예외이며 그 항목 모델의 유형은 StudentList.Model.Student
이어야하지만 HttpResponseMessage가 있습니다.
웹 서비스에서 데이터를 가져 오는 방법은 "StudentList.Model.Student"
유형으로 변환하고 View()
으로 지정 하시겠습니까?
패키지 nuget Newtonsoft를 사용할 수 있습니까? 그것은 JSON 문자열, XML인가요? 귀하의 API는 무엇을 반환합니까? – DGibbs
Im noob, 나는 API가 무엇을 반환하는지 알지 못하고 어떻게 확인할 수 있는지 잘 모릅니다. –