ASP.NET URL 中页面名称后所有路径的核心 Razor 页面端点路由
ASP.NET Core Razor Pages Endpoint Routing for all Paths after Page Name in URL
所以,我正在将 MCV 示例改编为 Razor Pages 应用程序,现在一切正常,除了端点路由。
期望的行为:
All text in the URL after the desired page name will be passed to that page's OnGet action as an input variable.
e.g. HTTP://application.tld/Reports/Static/any/text/that/follows
is handle by the Static.cshtml.cs page's OnGet(string viewPath)
and passes "any/text/that/follows" as the viewPath input variable
实际行为:
It tries to find a page at the full location, /Reports/Static/any/text/that/follows, which doesn't exist, so it returns a 404 error.
使用:
- Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation 版本="5.0.3"
- Microsoft.AspNetCore.Session 版本="2.2.0"
- Microsoft.EntityFrameworkCore 版本="5.0.3"
在 MCV 示例应用中 startup.cs:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
//Because everything after "Explorer/" is our path and path contains
//some slashes and maybe spaces, so we can use "Explorer/{*path}"
routes.MapRoute(
name: "Explorer",
template: "Explorer/{*path}",
defaults: new { controller = "Explorer", action = "Index" });
});
然后在控制器中
public IActionResult Index(string path)
在 Razor Pages 应用程序中 startup.cs:
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllerRoute(
name: "static",
pattern: "/Reports/Static/{*viewPath}",
defaults: new { page = "/Reports/Static", action = "OnGet" });
});
然后在页面中
public IActionResult OnGet(string viewPath)
关于如何让它工作的建议?
TIA:0)
使用我的最终解决方案进行编辑,非常感谢@Sergey,并且由于分段的数量是可变的,所以使用了一个包罗万象的解决方案:
(1) startup.cs 不需要这样做,所以我删除了“endpoints.MapControllerRoute()”部分
(2) 保留在页面的 cshtml.cs 文件中
public IActionResult OnGet(string viewPath)
(3) 然后在页面的cshtml文件中添加
@page "{*viewPath}"
如果您使用 razor 页面,则必须将其放在页面顶部:
@page "{any}/{text}/{that}/{follows}"
//or you can use some nullables
@page "{any}/{text?}/{that?}/{follows?}"
````
in the code behind the page in the action OnGet or OnGetAsync:
````
public async Tast<IActionResult> OnGetAsync( string any, string text, ....)
````
所以,我正在将 MCV 示例改编为 Razor Pages 应用程序,现在一切正常,除了端点路由。
期望的行为:
All text in the URL after the desired page name will be passed to that page's OnGet action as an input variable.
e.g. HTTP://application.tld/Reports/Static/any/text/that/follows is handle by the Static.cshtml.cs page's OnGet(string viewPath) and passes "any/text/that/follows" as the viewPath input variable
实际行为:
It tries to find a page at the full location, /Reports/Static/any/text/that/follows, which doesn't exist, so it returns a 404 error.
使用:
- Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation 版本="5.0.3"
- Microsoft.AspNetCore.Session 版本="2.2.0"
- Microsoft.EntityFrameworkCore 版本="5.0.3"
在 MCV 示例应用中 startup.cs:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
//Because everything after "Explorer/" is our path and path contains
//some slashes and maybe spaces, so we can use "Explorer/{*path}"
routes.MapRoute(
name: "Explorer",
template: "Explorer/{*path}",
defaults: new { controller = "Explorer", action = "Index" });
});
然后在控制器中
public IActionResult Index(string path)
在 Razor Pages 应用程序中 startup.cs:
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllerRoute(
name: "static",
pattern: "/Reports/Static/{*viewPath}",
defaults: new { page = "/Reports/Static", action = "OnGet" });
});
然后在页面中
public IActionResult OnGet(string viewPath)
关于如何让它工作的建议?
TIA:0)
使用我的最终解决方案进行编辑,非常感谢@Sergey,并且由于分段的数量是可变的,所以使用了一个包罗万象的解决方案:
(1) startup.cs 不需要这样做,所以我删除了“endpoints.MapControllerRoute()”部分
(2) 保留在页面的 cshtml.cs 文件中
public IActionResult OnGet(string viewPath)
(3) 然后在页面的cshtml文件中添加
@page "{*viewPath}"
如果您使用 razor 页面,则必须将其放在页面顶部:
@page "{any}/{text}/{that}/{follows}"
//or you can use some nullables
@page "{any}/{text?}/{that?}/{follows?}"
````
in the code behind the page in the action OnGet or OnGetAsync:
````
public async Tast<IActionResult> OnGetAsync( string any, string text, ....)
````