【问题标题】:How to grant access to only the routes defined?如何仅授予对定义的路由的访问权限?
【发布时间】:2018-04-15 14:53:14
【问题描述】:

我在 Startup.cs 中定义了以下代码:

services.AddMvc().AddRazorPagesOptions(options =>
{
    options.Conventions.AddPageRoute("/ListVehicles", "/vehicle-list");
});

如何只允许通过使用 url “vehicle-list”而不是在 url 中输入 cshtml 文件名 ListVehicles 来访问页面?我尝试了 options.Conventions.Clear() 但没有奏效。

【问题讨论】:

    标签: asp.net-core asp.net-core-mvc razor-pages


    【解决方案1】:

    您可以使用自定义IPageRouteModelConvention 来实现此目的,该Selectors 列表在所需的PageRouteModel 中清除:

    services.AddMvc().AddRazorPagesOptions(options =>
    {
        options.Conventions.AddPageRouteModelConvention("/ListVehicles", model =>
        {
            model.Selectors.Clear();
        });
        options.Conventions.AddPageRoute("/ListVehicles", "vehicle-list");
    });
    

    现在请求http://localhost/ListVehicles会导致404错误,而请求http://localhost/vehicle-list会返回ListVehicles.cshtml页面。

    【讨论】:

      猜你喜欢
      • 2020-04-04
      • 1970-01-01
      • 1970-01-01
      • 2020-01-07
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-18
      相关资源
      最近更新 更多