针对属性路由可以透过Route() 或 Http verbs方式来设置路由模板
在路由模板当中我们还能够透过"约束路由"来对参数进行约束。
语法格式
参数:约束
若没有做一些格式约束往往会造成系统漏洞
甚至可能会让系统出错的问题
约束种类共分为以下几种
1.参数约束
2.函数约束
3.正规表示式约束
1.参数约束
以之前的范例就是要以int 来做person的id查询
我们这里就约束参数必为int
[HttpGet("GetPersonById/{id:int}")]
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NetCoreApiTest1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace NetCoreApiTest1.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class StudentController : ControllerBase
{
[HttpGet("GetPersonById/{id:int}")]
public ActionResult<Person> GetPerson(int id)
{
List<Person> lsPersons = new List<Person>()
{
new Person(){Id=1,Name="Amy",Age=22,Sex="woman"},
new Person(){Id=2,Name="Jessica",Age=25,Sex="woman"},
new Person(){Id=3,Name="Banson",Age=24,Sex="man"}
};
return lsPersons.Where(item => item.Id == id).FirstOrDefault();
}
}
}
2.函数约束
可针对传入的参数透过函数约束的计算来作条件的过滤操作
以我们的Person List来说目前最大只有到3最小则是1
就可以透过以下这些含数约束语法做限制
{id:min(1)}
[HttpGet("GetPersonById/{id:min(1)}")]
{id:max(3)}
[HttpGet("GetPersonById/{id:max(3)}")]
当然也可以两个复合写
[HttpGet("GetPersonById/{id:min(1):max(3)}")]
或者也可以跟参数格式约束复合搭配然後用range设置上下限约束
{id:max:range(1,3)}
[HttpGet("GetPersonById/{id:int:range(1,3)}")]
也有一样效果
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NetCoreApiTest1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace NetCoreApiTest1.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class StudentController : ControllerBase
{
//[HttpGet("GetPersonById/{id:min(1)}")]
//[HttpGet("GetPersonById/{id:max(3)}")]
//[HttpGet("GetPersonById/{id:min(1):max(3)}")]
[HttpGet("GetPersonById/{id:int:range(1,3)}")]
public ActionResult<Person> GetPerson(int id)
{
List<Person> lsPersons = new List<Person>()
{
new Person(){Id=1,Name="Amy",Age=22,Sex="woman"},
new Person(){Id=2,Name="Jessica",Age=25,Sex="woman"},
new Person(){Id=3,Name="Banson",Age=24,Sex="man"}
};
return lsPersons.Where(item => item.Id == id).FirstOrDefault();
}
}
}
3.正规表示式约束
比方传入进来的参数代表的可能是名字
不可以是整数或者小数之类的只能是字串可能大小写a~z组合
或者年龄只能是纯数字
这里顺带提及到正规表示式在写的时候
由於有些会有方括号
刚好会跟上一篇提到的Route Template中占位符里面的
中方括号[] , 又固定只包含三种关键字[控制器]、[动作]、[区域]
[controller] , [action] , [area]
这个有冲突
因此在写得时候要用双重中方括号来区分是属於正规表示式的函数约束
不然直接用一般正规表示式写就可能会报
Replacement value for the token could not be found的错误
以下是测试程序码
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NetCoreApiTest1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace NetCoreApiTest1.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class StudentController : ControllerBase
{
//[HttpGet("GetPersonById/{id:min(1)}")]
//[HttpGet("GetPersonById/{id:max(3)}")]
//[HttpGet("GetPersonById/{id:min(1):max(3)}")]
[HttpGet("GetPersonById/{id:int:range(1,3)}")]
public ActionResult<Person> GetPerson(int id)
{
List<Person> lsPersons = new List<Person>()
{
new Person(){Id=1,Name="Amy",Age=22,Sex="woman"},
new Person(){Id=2,Name="Jessica",Age=25,Sex="woman"},
new Person(){Id=3,Name="Banson",Age=24,Sex="man"}
};
return lsPersons.Where(item => item.Id == id).FirstOrDefault();
}
[HttpGet("GetPersonByAge/{age:regex(\\d)}")]
public ActionResult<Person> GetPersonUseAge(int age)
{
List<Person> lsPersons = new List<Person>()
{
new Person(){Id=1,Name="Amy",Age=22,Sex="woman"},
new Person(){Id=2,Name="Jessica",Age=25,Sex="woman"},
new Person(){Id=3,Name="Banson",Age=24,Sex="man"}
};
return lsPersons.Where(item => item.Age == age).FirstOrDefault();
}
[HttpGet("GetPersonByName/{name:regex([[^0-9)]])}")]
public ActionResult<Person> GetPersonUseName(string name)
{
List<Person> lsPersons = new List<Person>()
{
new Person(){Id=1,Name="Amy",Age=22,Sex="woman"},
new Person(){Id=2,Name="Jessica",Age=25,Sex="woman"},
new Person(){Id=3,Name="Banson",Age=24,Sex="man"}
};
return lsPersons.Where(item => item.Name == name).FirstOrDefault();
}
}
}
Ref:
Route Constraints in ASP.NET Core Web API
https://dotnettutorials.net/lesson/route-constraints-asp-net-core-web-api/
Route Constraints In ASP.net Core
https://dotnetcoretutorials.com/2017/06/25/route-constraints-asp-net-core/
WebAPI Regex path attribute throws error "Replacement value for the token could not be found"
https://stackoverflow.com/questions/60726746/webapi-regex-path-attribute-throws-error-replacement-value-for-the-token-could
本篇已同步发表至个人部落格
https://coolmandiary.blogspot.com/2021/09/net-core-web-api10.html
就像稻米之於米苔目,小麦之於面疙瘩,原始资料就是任何资料产品最基础的存在。 在研究所修读统计的时候,...
在连资料库的时候 跑出这个错误 请问各位大大是哪里有问题? 这是错误的程序 说第八行可是我不知道哪...
其实任何技术上的选型都没有最好,就像选择程序语言一样,大家都有共识 PHP 是世界上最好的...咳,...
前言 各位早安,书接上回我们完成了Visual Studio Code的设定,已经进行到可以正式写程...
前言 制作大盘的讯号灯,当作熟悉讯号灯的操作方式。 本日程序码使用:d20_singalTaiexA...