Razor Pages 自定义页面路由
Razor Pages Custom Page Routing
之前有人问过这个问题,因为我是 Razor Pages
的新手,无法弄清楚如何才能完成所需的(愚蠢的)。所以这里是:我有一个 razor page
,其中显示了一个数据列表,我必须在其中进行页面路由或 url,如下所示:
@foreach (var item in Model.Data)
{
<a href="./search_details/id/@item.Id">@item.Name @item.City @item.State</a>
}
非常简单,所以在 Startup.cs
中,我尝试执行以下操作以使其工作。不幸失败:
//This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc();
services.AddEntityFrameworkSqlite().AddDbContext<MyDbContext>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddMvc().AddRazorPagesOptions(options =>
{
//Here is the configured routing that I tried
options.Conventions.AddPageRoute("/search_details", "/search_details/id");
});
}
我想 return url 作为 ./search_details/id/1002 并在后端执行此操作以获取查询字符串值:
string id = this.RouteData.Values["id"].ToString();
这是非常基本的,但正如我所说的失败(失败 意味着在后端,查询字符串在调试时没有命中)。我还有一种情况,这件事现在最困扰我。我的要求也是隐藏页面名称。像这样:
localhost:8080/search_details/id/1002
到
localhost:8080/1002
有没有合适的方法可以做到这一点?我知道这对于其他目的来说是一种不好的做法,但暂时我会接受它。
N.B:除非没有替代品,否则我不愿意在客户端做。如果可能的话,最好在服务器端使用 C#
.
您可以在 Razor 页面中使用路由模板:
https://www.learnrazorpages.com/razor-pages/routing
注释掉这一行 options.Conventions.AddPageRoute
,并在您的 search_details
页面中添加可为空的参数:
@page "{id?}"
在 cs 文件中,您可以获得如下路线数据:
public void OnGet(string id)
{
}
对于第二个要求,您可以添加如下模板:
options.Conventions.AddPageRoute("/search_details", "{id}");
这样 localhost:8080/1002
将重定向到 search_details
页面,您还将获得 id
作为路由数据。
之前有人问过这个问题,因为我是 Razor Pages
的新手,无法弄清楚如何才能完成所需的(愚蠢的)。所以这里是:我有一个 razor page
,其中显示了一个数据列表,我必须在其中进行页面路由或 url,如下所示:
@foreach (var item in Model.Data)
{
<a href="./search_details/id/@item.Id">@item.Name @item.City @item.State</a>
}
非常简单,所以在 Startup.cs
中,我尝试执行以下操作以使其工作。不幸失败:
//This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc();
services.AddEntityFrameworkSqlite().AddDbContext<MyDbContext>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddMvc().AddRazorPagesOptions(options =>
{
//Here is the configured routing that I tried
options.Conventions.AddPageRoute("/search_details", "/search_details/id");
});
}
我想 return url 作为 ./search_details/id/1002 并在后端执行此操作以获取查询字符串值:
string id = this.RouteData.Values["id"].ToString();
这是非常基本的,但正如我所说的失败(失败 意味着在后端,查询字符串在调试时没有命中)。我还有一种情况,这件事现在最困扰我。我的要求也是隐藏页面名称。像这样:
localhost:8080/search_details/id/1002
到
localhost:8080/1002
有没有合适的方法可以做到这一点?我知道这对于其他目的来说是一种不好的做法,但暂时我会接受它。
N.B:除非没有替代品,否则我不愿意在客户端做。如果可能的话,最好在服务器端使用 C#
.
您可以在 Razor 页面中使用路由模板:
https://www.learnrazorpages.com/razor-pages/routing
注释掉这一行 options.Conventions.AddPageRoute
,并在您的 search_details
页面中添加可为空的参数:
@page "{id?}"
在 cs 文件中,您可以获得如下路线数据:
public void OnGet(string id)
{
}
对于第二个要求,您可以添加如下模板:
options.Conventions.AddPageRoute("/search_details", "{id}");
这样 localhost:8080/1002
将重定向到 search_details
页面,您还将获得 id
作为路由数据。