我需要帮助。我正在创建一个MVC 6应用程序,但无法理解如何从REST服务中使用JSON。我找不到连接我的项目和服务然后消耗它的方法。在之前的版本中,无法添加服务引用。我在ASP.NET 5文档中找不到有关在MVC 6中使用第三方服务的政策。请问是否有人遇到相同问题?
public YourModel MakeRequest(string requestUrl)
{
try
{
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
if (response.StatusCode != HttpStatusCode.OK)
{
throw new Exception(String.Format("Server error (HTTP {0}: {1}).", response.StatusCode, response.StatusDescription));
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
var responseObject = serializer.Deserialize<YourModel>(response);
return responseObject;
}
}
catch (Exception e)
{
// catch exception and log it
return null;
}
}