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) { ... }
    }
  • 相关阅读:
    JUnit4的使用
    Android中使用JUnit4测试发生fatal error
    计算器的M+是什么意思
    初识Ildasm.exe——IL反编译的实用工具
    jsp下载
    jsp文件上传
    java.sql.SQLException: Io 异常:
    在PowerDesigner中创建物理模型时DBMS选项为空
    oracle10g还原被drop的表
    oracle创建用户
  • 原文地址:https://www.cnblogs.com/a14907/p/5099398.html
Copyright © 2011-2022 走看看