MVC引用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Week3.Model;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading.Tasks;
namespace Week3.MVC.Controllers
{
public class InfoController : Controller
{
private const string BaseUrl = "http://localhost:54982/api/";
// GET: Info //添加视图
public ActionResult AddInfoIndex()
{
return View();
}
[HttpPost]
public ActionResult AddInfo(InfoModel infoModel)
{
//实例化
HttpClient client = new HttpClient();
//赋值API地址
client.BaseAddress = new Uri(BaseUrl);
//完成调用WEBAPI AddUserInfo 的方法
var result = client.PostAsJsonAsync("AddUserInfo", infoModel).Result; //.Content.ReadAsStringAsync().Result;
//请求是否成功 成功 200
if (result.IsSuccessStatusCode)
{
var flag = result.Content.ReadAsStringAsync().Result;
if (Convert.ToInt32(flag) > 0)
{
return Content("<script>alert('添加成功')</script>");
}
else
{
return Content("<script>alert('添加失败')</script>");
}
}
else
{
return Content("<script>alert('HTTP响应失败')</script>");
}
}
//分页显示
public ActionResult PageInfoIndex()
{
return View();
}
//编辑
public ActionResult EditInfoIndex()
{
return View();
}
}
}

浙公网安备 33010602011771号