How to Migrate from WCF Web API to ASP.NET Web API
Posted on 2012-03-07 12:15 如是如是 阅读(392) 评论(0) 编辑 收藏 举报Introduction
We recently announced that WCF Web API is now ASP.NET Web API. This document provides guidance on how to migrate your existing WCF Web API code to ASP.NET Web API.
The WCF Web API abstractions map to ASP.NET Web API roughly as follows
WCF Web API | ASP.NET Web API |
Service | Web API controller |
Operation | Action |
Service contract | Not applicable |
Endpoint | Not applicable |
URI templates | ASP.NET Routing |
Message handlers | Same |
Formatters | Same |
Operation handlers | Filters, model binders |
Model Binding
1 2 3 4 5 6 7 | public HttpResponseMessage<comment> PostComment(Comment comment) { comment = repository.Add(comment); var response = new HttpResponseMessage<comment>(comment, HttpStatusCode.Created); response.Headers.Location = new Uri(Request.RequestUri, "/api/comments/" + comment.ID.ToString()); return response; } |
Filters
Filters are a really powerful feature in ASP.NET MVC. You can use attributes to apply pre/post logic to action methods, entire controllers, or globally to all action methods. They're available in ASP.NET Web API as well, and you use the same kind of logic to both build and apply them. I worked with a sample that applied some custom attribute based validation using a global action filter, and found it really to apply my ASP.NET MVC background.