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) { ... }
    }
  • 相关阅读:
    python 2 和 python 3 的区别
    random模块、time模块、sys模块、os模块
    正则表达式
    生成器 推导式 生成器表达式
    免费的论文查重网站
    Django 13
    pycharm连接mysql出错解决方案
    前端 51
    前端 50
    前段 49
  • 原文地址:https://www.cnblogs.com/a14907/p/5099398.html
Copyright © 2011-2022 走看看