作者:秋名
撰写时间:2020 年 9 月 19 日
技术要点:预防前端通过路径传入非法字符到后端,导致数据奔溃,在后台使用try{}catch{}进行捕获非法字符。
使用前端Vue+后台.Net Core3.0+vs2019+SQLserver
methods: {
getProductById(pid) {
var thisVue = this;
this.$http
.get("https://localhost:44310/api/Products/getProductById/?id=" + pid)
.then(function (res) {
console.log(res.status)
if(res.status==205){//当接收status值为205,将跳转至其他页面。
thisVue.$router.push("/ErrPage");
}
thisVue.product = res.data;
});
},
public Products GetProductById(string id) {
//为了方法容措性更好 方法后的参数建议string类型
int pid = -1;
try
{
pid= Convert.ToInt32(id);
}
catch (Exception)
{
//HTTP响应状态码 ,返回页面 403(拒绝访问)
HttpContext.Response.StatusCode = 205;
return default;//返回空
throw;
}
List<Products> productList = Products.GetProductList();
return productList.Find(m => m.Id == pid);
}