如何验证 GET 方法中的查询字符串参数

How to validate query string parameters in GET method

我已经构建了一个 ASP.NET Core MVC 应用程序并且我使用了默认的 MVC URL 路由:

app.UseMvc(routes =>
{
    routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});

我想创建 GET 方法来创建和编辑用户。

要创建新用户,我需要转到 url /Admin/User 并编辑现有用户 url /Admin/User/{id:int}

我试过创建这样的方法:

[HttpGet]
public async Task<IActionResult> User(int? id)
{
}

如何限制id参数的类型?

我只需要允许访问 /Admin/User/Admin/User/{id:int} - 但如果我尝试即 - /Admin/User/ABCD - (使用字符串作为 ID) - 它也是允许的。

如果参数 ID 是另一种类型而不是数字那么我想 return 404.

您必须为此编写一个自定义模型联编程序,或者为 returns 404 等所有其他类型创建一个重载。我会简单地重新考虑您的设计并采取不同的 /Admin/CreateUser 操作用于创建新用户,只是不允许没有 ID 的用户调用。

使用单一方法是可以实现的。

如果您使用数字 ID,则现有用户 ID 将大于零。对于新用户 ID 将为零或小于零。

如果您使用字符串 ID,那么对于现有用户,ID 将为 non-empty 字符串。例如。 A B C D。对于新用户 ID 将为空。

基于此逻辑,您可以发送适当的响应或 404 响应。 如果您使用的是字符串 ID,请更改 Get 方法的数据类型。而不是使用可为空的 int 使用字符串。

以下代码可能会有所帮助

[HttpGet("{id}")]
    public async Task<IActionResult> User(string id)
    {
        if (string.IsNullOrWhiteSpace(id))
        {
            //Replace NewUser view name by Appropriate View name in your project
            return View("NewUser");
        }
        else
        {
           var isValidUser= IsValidUser(id);
            if(isValidUser)
            {
                //Replace ExistingUser view name by Appropriate View name in your project
                return View("ExistingUser");
            }
            else
            {
                //User Appropriate overload of NotFound
                return NotFound();
            }
        }
    }
    private bool IsValidUser(string userId)
    {
        //Your logic to validate the existing user.            
    }

最简单的选择是创建具有此约束的路由。请注意,如果您像这样在路由中设置所需的参数,则没有理由使 id 可为空。

[HttpGet("Admin/User")]
public async Task<IActionResult> Add()
{
}

[HttpGet("Admin/User/{id:int}")]
public async Task<IActionResult> Edit(int id) 
{
}

或者保留名为 User 的操作方法并使用:

app.UseMvc(routes =>
{
    routes.MapRoute("AdminUser", "Admin/User/{id:int}");
    routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});

Note that the convention-based approach doesn't differentiate between GET/POST.

或者,如果您想将所有 ID 参数设为 int,您可以像这样为整个站点配置路由:

routes.MapRoute(
    name: "defaultWithId",
    template: "{controller}/{action}/{id:int}");
routes.MapRoute(
    name: "default",
    template: "{controller=Home}/{action=Index}");

参考:Routing in ASP.NET Core