firebird
firebird

Reputation: 3521

How to grant access to only the routes defined?

I have the following code defined in Startup.cs:

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

How do I only allow access to the page by using the url "vehicle-list" instead of just typing the cshtml file name ListVehicles in the url? I tried options.Conventions.Clear() but that didn't work.

Upvotes: 1

Views: 103

Answers (1)

CodeFuller
CodeFuller

Reputation: 31312

You could achieve this with custom IPageRouteModelConvention that clears Selectors list in required PageRouteModel:

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

Now request to http://localhost/ListVehicles will result to 404 error, while request to http://localhost/vehicle-list will return ListVehicles.cshtml page.

Upvotes: 1

Related Questions