我在过去创建了几个WebAPIs应用程序,但我以前从未使用过ASP.NET。我想知道如何才能创建一个简单的MVC4应用程序,通过WebAPI而不是普通的MVC控制器来执行简单的CRUD操作。诀窍是WebAPI应该是一个单独的解决方案(实际上,很可能位于不同的服务器/域上)。
我该怎么做?我遗漏了什么?仅仅是设置指向WebAPI服务器的路由吗?我找到的所有展示如何使用MVC应用程序使用WebAPI的示例似乎都假定WebAPI是“内置”到MVC应用程序中的,或者至少在同一服务器上。
哦,澄清一下,我不是在谈论使用jQuery的Ajax调用...我的意思是,MVC应用程序的控制器应该使用WebAPI来获取/放置数据。
发布于 2012-11-03 14:25:21
您应该使用新的HttpClient来使用您的HTTP。我可以另外建议你让你的调用完全异步。由于ASP.NET MVC控制器操作支持基于任务的异步编程模型,所以它非常强大且简单。
这是一个过于简单的例子。以下代码是一个示例请求的helper类:
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控制器异步使用它,如下所示:
public class HomeController : Controller {
private CarRESTService service = new CarRESTService();
public async Task<ActionResult> Index() {
return View("index",
await service.GetCarsAsync()
);
}
}
你可以看看下面的文章,看看ASP.NET MVC异步I/O操作的效果:
My Take on Task-based Asynchronous Programming in C# 5.0 and ASP.NET MVC Web Applications
发布于 2012-11-06 02:50:14
感谢大家的回复。@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);
}
}
发布于 2012-11-03 17:07:44
您可以使用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中放置端点的配置:
<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>
https://stackoverflow.com/questions/13200381
复制相似问题