asp.net web api rest风格与RPC风格调用
2012-12-14 09:43
501 查看
Working console program:
Working Controller
static voidMain(string[] args) { // Set up server configuration HttpSelfHostConfiguration config =newHttpSelfHostConfiguration("http://localhost:8080"); //Route Catches the GET PUT DELETE typical REST based interactions (add more if needed) config.Routes.MapHttpRoute("API Default","api/{controller}/{id}",new{ id =RouteParameter.Optional},new{ httpMethod =newHttpMethodConstraint(HttpMethod.Get,HttpMethod.Put,HttpMethod.Delete)}); //This allows POSTs to the RPC Style methods http://api/controller/action config.Routes.MapHttpRoute("API RPC Style","api/{controller}/{action}",new{ httpMethod =newHttpMethodConstraint(HttpMethod.Post)}); //Finally this allows POST to typeical REST post address http://api/controller/ config.Routes.MapHttpRoute("API Default 2","api/{controller}/{action}",new{ action ="Post"},new{ httpMethod =newHttpMethodConstraint(HttpMethod.Post)}); using(HttpSelfHostServer server =newHttpSelfHostServer(config)) { server.OpenAsync().Wait();Console.WriteLine("Press Enter to quit.");Console.ReadLine(); } }
Working Controller
public class TaskInstanceQueueController:ApiController { publicvoidGet(string id) { // Do something with my taskInstanceConsole.WriteLine("Method entered!"+ id);}[ActionName("Post")][HttpPost]publicvoidPost(TaskInstance taskInstance){// Do something with my taskInstanceConsole.WriteLine("REST Post Method entered!");}[ActionName("Queue")][HttpPost]publicvoidQueue(TaskInstance taskInstance){// Do something with my taskInstanceConsole.WriteLine("Queue Method entered!");}[ActionName("Another")][HttpPost]publicvoidAnother(TaskInstance taskInstance){Console.WriteLine("Another Method entered!");}}
相关文章推荐
- 使用ASP.NET WEB API构建基于REST风格的服务实战系列教程(一)——使用EF6构建数据库及模型
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【二】——使用Repository模式构建数据库访问层
- ASP.NET Web Api构建基于REST风格的服务实战系列教程
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【六】——实现资源间的关联
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【八】——Web Api的安全性
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【三】——Web Api入门
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【二】——使用Repository模式构建数据库访问层
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【四】——实现模型工厂,依赖注入以及格式配置
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【三】——Web Api入门
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【十】——使用CacheCow和ETag缓存资源
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【五】——在Web Api中实现Http方法(Put,Post,Delete)
- [转]使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【八】——Web Api的安全性
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【十】——使用CacheCow和ETag缓存资源
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【六】——实现资源间的关联
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【外传】——Attribute Routing
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【七】——实现资源的分页
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【七】——实现资源的分页
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【五】——在Web Api中实现Http方法(Put,Post,Delete)