zoukankan      html  css  js  c++  java
  • webapi中的路由前缀

    Route Prefixes

    Often, the routes in a controller all start with the same prefix. For example:

    public class BooksController : ApiController
    {
        [Route("api/books")]
        public IEnumerable<Book> GetBooks() { ... }
    
        [Route("api/books/{id:int}")]
        public Book GetBook(int id) { ... }
    
        [Route("api/books")]
        [HttpPost]
        public HttpResponseMessage CreateBook(Book book) { ... }
    }

    You can set a common prefix for an entire controller by using the [RoutePrefix] attribute:

    [RoutePrefix("api/books")]
    public class BooksController : ApiController
    {
        // GET api/books
        [Route("")]
        public IEnumerable<Book> Get() { ... }
    
        // GET api/books/5
        [Route("{id:int}")]
        public Book Get(int id) { ... }
    
        // POST api/books
        [Route("")]
        public HttpResponseMessage Post(Book book) { ... }
    }

    Use a tilde (~) on the method attribute to override the route prefix:

    [RoutePrefix("api/books")]
    public class BooksController : ApiController
    {
        // GET /api/authors/1/books
        [Route("~/api/authors/{authorId:int}/books")]
        public IEnumerable<Book> GetByAuthor(int authorId) { ... }
    
        // ...
    }

    The route prefix can include parameters:

    [RoutePrefix("customers/{customerId}")]
    public class OrdersController : ApiController
    {
        // GET customers/1/orders
        [Route("orders")]
        public IEnumerable<Order> Get(int customerId) { ... }
    }
  • 相关阅读:
    day14: 生成器进阶
    day13: 迭代器和生成器
    day12:装饰器的进阶
    day11:装饰器
    day10:函数进阶
    English class 81:How Vulnerability can make our lives better?
    piano class 12
    UE4之循环
    UE4之数组
    UE4中常见的类
  • 原文地址:https://www.cnblogs.com/a14907/p/5099398.html
Copyright © 2011-2022 走看看