WebAPI v2介绍了
路由属性
这些可以与控制器类一起使用,并且可以方便地进行路由配置。
例如:
public class BookController : ApiController{
//where author is a letter(a-Z) with a minimum of 5 character and 10 max.
[Route("html/{id}/{newAuthor:alpha:length(5,10)}")]
public Book Get(int id, string newAuthor){
return new Book() { Title = "SQL Server 2012 id= " + id, Author = "Adrian & " + newAuthor };
}
[Route("json/{id}/{newAuthor:alpha:length(5,10)}/{title}")]
public Book Get(int id, string newAuthor, string title){
return new Book() { Title = "SQL Server 2012 id= " + id, Author = "Adrian & " + newAuthor };
}
...
但是,请注意查询参数
?var1=1&var2=2
不接受评估以决定将使用哪种API方法。
WebAPI
基于反射工作,因此,这意味着您的大括号变量在方法中必须匹配相同的名称。
所以要匹配这样的东西
api/Products/Product/test
您的模板应该如下所示
"api/{controller}/{action}/{id}"
您的方法需要这样声明:
[ActionName("Product")]
[HttpGet]
public object Product(string id){
return id;
}
其中参数
string name
被替换
string id
.