Asp.net Core 3.1- 我如何 return 从控制器到 Razor 页面?
Asp.net Core 3.1- How can I return from Controller to Razor Page?
我希望当用户登录到该站点时,在剃须刀页面中转到我想要的地址...
if (rolename == "User")
{
return RedirectToPage("Dashboard","Profile");
}
此代码无法正常工作...
错误信息如下:
InvalidOperationException: The relative page path 'Dashboard' can only be used while executing a Razor Page. Specify a root relative path with a leading '/' to generate a URL outside of a Razor Page. If you are using LinkGenerator then you must provide the current HttpContext to use relative pages.
如何做到这一点?
RedirectToPage 有多个不同参数的方法,您所做的与以下方法有关:
//
// Summary:
// Redirects (Microsoft.AspNetCore.Http.StatusCodes.Status302Found) to the specified
// pageName using the specified pageHandler.
//
// Parameters:
// pageName:
// The name of the page.
//
// pageHandler:
// The page handler to redirect to.
//
// Returns:
// The Microsoft.AspNetCore.Mvc.RedirectToPageResult.
[NonAction]
public virtual RedirectToPageResult RedirectToPage(string pageName, string pageHandler);
在你的情况下,它应该是:
return RedirectToPage("/Profile/Dashboard");
此外,请务必添加剃刀页面路由:
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//...
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Privacy}/{phrase?}");
endpoints.MapRazorPages();
});
}
我希望当用户登录到该站点时,在剃须刀页面中转到我想要的地址...
if (rolename == "User")
{
return RedirectToPage("Dashboard","Profile");
}
此代码无法正常工作...
错误信息如下:
InvalidOperationException: The relative page path 'Dashboard' can only be used while executing a Razor Page. Specify a root relative path with a leading '/' to generate a URL outside of a Razor Page. If you are using LinkGenerator then you must provide the current HttpContext to use relative pages.
如何做到这一点?
RedirectToPage 有多个不同参数的方法,您所做的与以下方法有关:
//
// Summary:
// Redirects (Microsoft.AspNetCore.Http.StatusCodes.Status302Found) to the specified
// pageName using the specified pageHandler.
//
// Parameters:
// pageName:
// The name of the page.
//
// pageHandler:
// The page handler to redirect to.
//
// Returns:
// The Microsoft.AspNetCore.Mvc.RedirectToPageResult.
[NonAction]
public virtual RedirectToPageResult RedirectToPage(string pageName, string pageHandler);
在你的情况下,它应该是:
return RedirectToPage("/Profile/Dashboard");
此外,请务必添加剃刀页面路由:
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//...
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Privacy}/{phrase?}");
endpoints.MapRazorPages();
});
}