zoukankan      html  css  js  c++  java
  • webapi中常用attribute标签

    HTTP Methods

    Instead of using the naming convention for HTTP methods, you can explicitly specify the HTTP method for an action by decorating the action method with the HttpGetHttpPutHttpPost, or HttpDelete attribute.

    In the following example, the FindProduct method is mapped to GET requests:

    public class ProductsController : ApiController
    {
        [HttpGet]
        public Product FindProduct(id) {}
    }

    To allow multiple HTTP methods for an action, or to allow HTTP methods other than GET, PUT, POST, and DELETE, use the AcceptVerbsattribute, which takes a list of HTTP methods.

    public class ProductsController : ApiController
    {
        [AcceptVerbs("GET", "HEAD")]
        public Product FindProduct(id) { }
    
        // WebDAV method
        [AcceptVerbs("MKCOL")]
        public void MakeCollection() { }
    }

    Routing by Action Name

    With the default routing template, Web API uses the HTTP method to select the action. However, you can also create a route where the action name is included in the URI:

    routes.MapHttpRoute(
        name: "ActionApi",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );

    In this route template, the {action} parameter names the action method on the controller. With this style of routing, use attributes to specify the allowed HTTP methods. For example, suppose your controller has the following method:

    public class ProductsController : ApiController
    {
        [HttpGet]
        public string Details(int id);
    }

    In this case, a GET request for “api/products/details/1” would map to the Details method. This style of routing is similar to ASP.NET MVC, and may be appropriate for an RPC-style API.

    You can override the action name by using the ActionName attribute. In the following example, there are two actions that map to "api/products/thumbnail/id. One supports GET and the other supports POST:

    public class ProductsController : ApiController
    {
        [HttpGet]
        [ActionName("Thumbnail")]
        public HttpResponseMessage GetThumbnailImage(int id);
    
        [HttpPost]
        [ActionName("Thumbnail")]
        public void AddThumbnailImage(int id);
    }

    Non-Actions

    To prevent a method from getting invoked as an action, use the NonAction attribute. This signals to the framework that the method is not an action, even if it would otherwise match the routing rules.

    // Not an action method.
    [NonAction]  
    public string GetPrivateData() { ... }
  • 相关阅读:
    20.C语言_数组参数传递
    19.C语言_取值运算符*
    Google Chrome 解决 “您的连接不是私密连接” 和被毒霸劫持
    教你如何一步步将项目部署到Github
    教你如何把Android手机的网络完全映射到PC上,比如免流给PC用…
    CSS background-size 属性详解
    display:block;是什么意思
    Cookie是储存在电脑文本文件中的数据,用于保存访问者的信息,并可以在下次打开页面时引用。
    Marquee(跑马灯)横向、纵向、无空白的不间断连续循环滚动代码
    Visual Studio中从应用程序中调试SQL脚本
  • 原文地址:https://www.cnblogs.com/a14907/p/5098823.html
Copyright © 2011-2022 走看看