zoukankan      html  css  js  c++  java
  • asp.net web api rest风格与RPC风格调用

    Working console program:

    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!");}}
  • 相关阅读:
    bzoj2143 飞飞侠
    Codeforces 543.B Destroying Roads
    Codeforces 666.B World Tour
    bzoj2441 [中山市选2011]小W的问题(debug中)
    bzoj2329 [HNOI2011]括号修复
    一些新发现的好东西
    < meta http-equiv = "X-UA-Compatible" content = "IE=edge,chrome=1" />的作用
    js经典代码技巧学习之一:使用三元运算符处理javascript兼容
    《javascript高级程序设计》笔记4.1.4:检测类型
    页面关闭和刷新事件
  • 原文地址:https://www.cnblogs.com/fx2008/p/2817504.html
Copyright © 2011-2022 走看看