gpt4 book ai didi

asp.net-mvc - Asp.net mvc 3 处理意外查询

转载 作者:行者123 更新时间:2023-12-03 07:40:36 25 4
gpt4 key购买 nike

我只是想知道用户可能通过意外查询请求的情况。假设我有 Controller Action

  public ViewResult Details(int id)
{
Description description = db.Descriptions.Find(id);
return View(description);
}

浏览器中的理想查询将是 /admin/Details?id=1 .
如果用户输入 id=-1 怎么办?或 id=a或任何其他意外输入。如何处理?

最佳答案

为确保数值,您可以添加 id = @"\d+"路线约束,只有在请求 id 时才会执行您的操作是数字,否则返回http not found;

在所有其他情况下,您应该始终检查用户输入,如下所示:

public ActionResult Details(int id)
{
Description description = db.Descriptions.Find(id);

if(description == null)
{
return new HttpStatusCodeResult(404);
}

return View(description);
}

并且用户将被通知他请求的资源具有无效标识符

关于asp.net-mvc - Asp.net mvc 3 处理意外查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11861813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com