Gle*_*ndt 60 asp.net-mvc asp.net-mvc-4 asp.net-web-api
我以前创建了几个ASP.NET MVC应用程序,但我以前从未使用过WebAPI.我想知道如何创建一个简单的MVC 4应用程序,通过WebAPI而不是通过普通的MVC控制器来完成简单的CRUD.诀窍是WebAPI应该是一个单独的解决方案(事实上,很可能是在不同的服务器/域上).
我怎么做?我错过了什么?是否只是设置路由指向WebAPI的服务器?我发现的所有示例都展示了如何使用MVC应用程序来使用WebAPI似乎假设WebAPI已经"融入"MVC应用程序,或者至少在同一台服务器上.
哦,澄清一下,我不是在谈论使用jQuery的Ajax调用......我的意思是MVC应用程序的控制器应该使用WebAPI来获取/放置数据.
tug*_*erk 82
您应该使用新的HttpClient来使用您的HTTP API.我还可以建议您完全异步调用.由于ASP.NET MVC控制器操作支持基于任务的异步编程模型,因此它非常强大且简单.
这是一个过于简化的例子.以下代码是示例请求的帮助程序类:
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)
);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我可以通过我的MVC控制器异步消耗它,如下所示:
public class HomeController : Controller {
private CarRESTService service = new CarRESTService();
public async Task<ActionResult> Index() {
return View("index",
await service.GetCarsAsync()
);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以查看下面的帖子,了解ASP.NET MVC的异步I/O操作的效果:
我在C#5.0和ASP.NET MVC Web应用程序中采用基于任务的异步编程
Gle*_*ndt 18
谢谢大家的回复.我认为@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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后是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);
}
}
Run Code Online (Sandbox Code Playgroud)
pec*_*eco 10
You can use WCF to consume the service. Like so:
[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();
}
}
Run Code Online (Sandbox Code Playgroud)
And then you can consume it in your controller:
public class HomeController : Controller
{
public HomeController()
{
}
public ActionResult List()
{
var service = new DogServiceClient("YourEndpoint");
var dogs = service.List();
return View(dogs);
}
}
Run Code Online (Sandbox Code Playgroud)
And in your web.config you place the configuration for your endpoint:
<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>
Run Code Online (Sandbox Code Playgroud)