ASP.NET MVC 4应用程序调用远程WebAPI

过去我创build了几个ASP.NET MVC应用程序,但是我从来没有使用过WebAPI。 我想知道如何创build一个简单的MVC 4应用程序,通过WebAPI,而不是通过一个正常的MVC控制器做简单的CRUD的东西。 诀窍是WebAPI应该是一个单独的解决scheme(事实上,它可能在不同的服务器/域上)。

我怎么做? 我错过了什么? 这只是一个设置路线指向WebAPI的服务器的问题? 我已经find的所有示例展示了如何使用MVC应用程序来使用WebAPI,似乎认为WebAPI被“烘焙”到MVC应用程序中,或者至less在同一台服务器上。

哦,并澄清,我不是在谈论使用jQuery的Ajax调用…我的意思是MVC应用程序的控制器应该使用WebAPI来获取/放置数据。

你应该使用新的HttpClient来使用你的HTTP API。 我还可以build议你使你的调用完全asynchronous。 由于ASP.NET MVC控制器动作支持基于任务的asynchronous编程模型,它非常强大和容易。

这是一个非常简单的例子。 以下代码是示例请求的助手类:

public class CarRESTService { readonly string uri = "http://localhost:2236/api/cars"; public async Task<List<Car>> GetCarsAsync() { using (HttpClient httpClient = new HttpClient()) { return JsonConvert.DeserializeObject<List<Car>>( await httpClient.GetStringAsync(uri) ); } } } 

然后,我可以通过我的MVC控制器asynchronous消耗,如下所示:

 public class HomeController : Controller { private CarRESTService service = new CarRESTService(); public async Task<ActionResult> Index() { return View("index", await service.GetCarsAsync() ); } } 

你可以看看下面的post,看看ASP.NET MVC的asynchronousI / O操作的效果:

我在C#5.0和ASP.NET MVC Web应用程序中采用基于任务的asynchronous编程

感谢大家的回应。 @tugberk让我走了正确的道路,我想。 这对我工作…

对于我的CarsRESTService帮手:

 public class CarsRESTService { readonly string baseUri = "http://localhost:9661/api/cars/"; public List<Car> GetCars() { string uri = baseUri; using (HttpClient httpClient = new HttpClient()) { Task<String> response = httpClient.GetStringAsync(uri); return JsonConvert.DeserializeObjectAsync<List<Car>>(response.Result).Result; } } public Car GetCarById(int id) { string uri = baseUri + id; using (HttpClient httpClient = new HttpClient()) { Task<String> response = httpClient.GetStringAsync(uri); return JsonConvert.DeserializeObjectAsync<Car>(response.Result).Result; } } } 

然后为CarsController.cs:

 public class CarsController : Controller { private CarsRESTService carsService = new CarsRESTService(); // // GET: /Cars/ public ActionResult Index() { return View(carsService.GetCars()); } // // GET: /Cars/Details/5 public ActionResult Details(int id = 0) { Car car = carsService.GetCarById(id); if (car == null) { return HttpNotFound(); } return View(car); } } 

您可以使用WCF来使用该服务。 像这样:

 [ServiceContract] public interface IDogService { [OperationContract] [WebGet(UriTemplate = "/api/dog")] IEnumerable<Dog> List(); } public class DogServiceClient : ClientBase<IDogService>, IDogService { public DogServiceClient(string endpointConfigurationName) : base(endpointConfigurationName) { } public IEnumerable<Dog> List() { return Channel.List(); } } 

然后你可以在你的控制器中使用它:

 public class HomeController : Controller { public HomeController() { } public ActionResult List() { var service = new DogServiceClient("YourEndpoint"); var dogs = service.List(); return View(dogs); } } 

并在你的web.config中放置你的端点的configuration:

 <system.serviceModel> <client> <endpoint address="http://localhost/DogService" binding="webHttpBinding" bindingConfiguration="" behaviorConfiguration="DogServiceConfig" contract="IDogService" name="YourEndpoint" /> </client> <behaviors> <endpointBehaviors> <behavior name="DogServiceConfig"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> </system.serviceModel> 

http://restsharp.org/是你的问题的答案。; 我目前正在使用一个具有类似结构的应用程序。

但更一般地使用WebAPI只是发布和请求数据如何处理取决于你。 你甚至可以使用标准的WebRequest和JavascriptSerializer。

干杯。

在这种情况下,您可以使用HttpClient从您的控制器使用Web API。