为剃刀页面创建别名

Create an alias for razor page

我正在玩 asp.net razor pages。

我创建了默认 asp.net 核心 2.1 应用程序,然后添加了 3 个页面:

default convention 页面将在路径中可用:

Page1 -> http://localhost/Page1
Page2 -> http://localhost/Folder/Page2
Page3 -> http://localhost/Page3

现在我想为 page2 添加别名,例如像这样:

Page1 -> http://localhost/Page1
Page2 -> http://localhost/Page2
Page2 -> http://localhost/Folder/Page2
Page3 -> http://localhost/Page3

是否可以在没有 adding-file-as-link 的情况下为此页面创建额外的 route/alias?

在@page中指定页面路由

@page "/Page2"
@{
    ViewData["Title"] = "Page2";
}

<h2>Page2</h2>

使 page2 可用 http://localhost/Page2,但不可用 http://localhost/Folder/Page2 :(

这个,可能会有帮助。 https://github.com/T4MVC/T4MVC

T4MVC is a T4 template for ASP.NET MVC apps that creates strongly typed helpers that eliminate the use of literal strings in many places.

它将为您的视图生成文字路径,然后您可以 return 同一操作的不同视图。

if (someCondition){
    return View(this.ViewNames.Page2)
}
else {
    return View(this.ViewNames.Folder_Page2)
}

您可以使用 AddPageRoute(...) 实现此目的。这是您的情况的示例:

services
    .AddMvc()
    .AddRazorPagesOptions(options =>
    {
        options.Conventions.AddPageRoute("/Folder/Page2", "/Page2");
    });