ASP.NET CORE WebAPI 中 Route 属性配置

本文详细介绍了ASP.NET CORE WebAPI中关于路由的配置,包括访问路径属性Route、HTTP Request属性如何匹配请求类型、使用AcceptVerbs指定多个HTTP方法、设置路由前缀以及应用路由约束和路由名称的用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 访问路径属性  Route

public class OrdersController : ApiController
{
    [Route("customers/{customerId}/orders")]
    [HttpGet]
    public IEnumerable<Order> FindOrdersByCustomer(int customerId) { ... }
}

字符串"customers/{customerId}/orders"是路由模板。 Web API根据这个模板进行匹配。 在这个例子中, "customers" 和 "orders" 是文字描述部分,必须完全匹配,  "{customerId}" 是变量部分。

 

2.HTTP Request 属性

Web API 在选择action时,会基于HTTP的request类型的。

默认的,Web API 会匹配controller方法的开始部分,例如方法名称为PostCustomers 会匹配HTTP的Post request。

我们也可以通过方法修饰属性来控制request类型。

方法修饰属性 :[HttpDelete] [HttpGet] [HttpHead] [HttpOptions] [HttpPatch] [HttpPost] [HttpPut]

[Route("api/books")]
[HttpPost]
public HttpResponseMessage CreateBook(Book book) { ... }

以上代码控制HTTP的request类型为Post.

 

3.HTTP 方法列表属性 AcceptVerbs

[Route("api/books")]
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public void MakeCollection() { }

以上代码同时支持Get和Post.

 

4.路由前缀属性 Route Prefixes

[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) { ... }
}

用符号“~”覆盖路由前缀

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

    // ...
}

5 路由约束

通过路由约束来约束参数类型。语法{参数:字段类型}

[Route("users/{id:int}")]
public User GetUserById(int id) { ... }

[Route("users/{id:int:min(1)}")]
public User GetUserById(int id) { ... }

//有默认值
public class BooksController : ApiController
{
    [Route("api/books/locale/{lcid:int=1033}")]
    public IEnumerable<Book> GetBooksByLocale(int lcid) { ... }
}

支持的约束类型:

ConstraintDescriptionExample
alphaMatches uppercase or lowercase Latin alphabet characters (a-z, A-Z){x:alpha}
boolMatches a Boolean value.{x:bool}
datetimeMatches a DateTime value.{x:datetime}
decimalMatches a decimal value.{x:decimal}
doubleMatches a 64-bit floating-point value.{x:double}
floatMatches a 32-bit floating-point value.{x:float}
guidMatches a GUID value.{x:guid}
intMatches a 32-bit integer value.{x:int}
lengthMatches a string with the specified length or within a specified range of lengths.{x:length(6)} {x:length(1,20)}
longMatches a 64-bit integer value.{x:long}
maxMatches an integer with a maximum value.{x:max(10)}
maxlengthMatches a string with a maximum length.{x:maxlength(10)}
minMatches an integer with a minimum value.{x:min(10)}
minlengthMatches a string with a minimum length.{x:minlength(10)}
rangeMatches an integer within a range of values.{x:range(10,50)}
regexMatches a regular expression.{x:regex(^\d{3}-\d{3}-\d{4}$)}

6 路由名称

在HTTP 调用方法时,可以使用路由名称调用。

[Route("api/books/{id}", Name="GetBookById")]
    public BookDto GetBook(int id) 
    {
        // Implementation not shown...
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值