How to Migrate from WCF Web API to ASP.NET Web API
2012-03-07 12:15
399 查看
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.
Overview
The WCF Web API abstractions map to ASP.NET Web API roughly as follows
Web API Controllers
Update you service types to be web API controllers by first deriving from ApiController and renaming your service type so that the type name ends with "Controller".
Routing
You can directly map URI templates to routes by defining a route that matches the URI template path and then specifying the method name as the default value for the action route variable. The approach will result in one route per action, which depending on the number of actions may result in significant performance overhead.
To consolidate the number of routes consider refactoring your service so that there is one web API controller per resource.
Model Binding
Model binding is a powerful feature in ASP.NET MVC (and on its way to Web Forms in ASP.NET 4.5 too). It allows you to write a method that accepts your custom object type as a parameter, and ASP.NET Web API handles mapping posted data to that parameter. It lets you focus on implementing your specifications and get out of the business of mindless (and error prone) mapping code. Here's an example, showing how a method that accepts posted data can focus on the logic of handling the data:
?
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.
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.
Overview
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 |
Update you service types to be web API controllers by first deriving from ApiController and renaming your service type so that the type name ends with "Controller".
Routing
You can directly map URI templates to routes by defining a route that matches the URI template path and then specifying the method name as the default value for the action route variable. The approach will result in one route per action, which depending on the number of actions may result in significant performance overhead.
To consolidate the number of routes consider refactoring your service so that there is one web API controller per resource.
Model Binding
Model binding is a powerful feature in ASP.NET MVC (and on its way to Web Forms in ASP.NET 4.5 too). It allows you to write a method that accepts your custom object type as a parameter, and ASP.NET Web API handles mapping posted data to that parameter. It lets you focus on implementing your specifications and get out of the business of mindless (and error prone) mapping code. Here's an example, showing how a method that accepts posted data can focus on the logic of handling the data:?
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.相关文章推荐
- How to Redirect in ASPNET Web API
- How to pass javascript complex object to ASP.NET Web Api and MVC
- how-to-add-global-asp-net-web-api-filters
- How do I get ASP.NET Web API to return JSON instead of XML using Chrome
- How to Host ASP.Net Web API on IIS Server
- 转:ASP.NET Web Services or .NET Remoting: How to Choose
- 在Asp.net应用程序中构建基于WCF Web.Api的服务
- ASP.NET MVC: how to include <span> in link from Html.ActionLink?
- How to invoke Java web service in ASP.net using C#
- How to get the Values of Selected Row from a Gridview using ASP.NET
- How to get all Errors from ASP.Net MVC modelState?
- How to use AspnetUpload™ in your web application
- Beginning Web Development, Silverlight, and ASP.NET AJAX: From Novice to Professional
- How to use FtpWebRequest in asp.net
- How to build Multi-Language Web Sites with ASP.NET 2.0 and VS.Net 2005
- 微软ASP.NET 电商网站开发实战 MVC6 +HTML5 +WCF+WebAPI+NoSQL+mongoDB+Redis+Core视频 代码 面试题
- Implementing & Consuming ASP.NET WEB API from JQuery (MVC 4)
- [GOOD Article] How to disable web.config Inheritance for Child Applications in Subfolders in ASP.NET?
- HOW TO: Upload a File to a Web Server in ASP.NET by Using Visual Basic .NET
- How to generate an RSS feed for your web site using ASP.NET