c# - ASP.net MVC core RedirectToPage错误-指定根相对路径错误

标签 c# asp.net-core-mvc razor-pages

我想从这样的普通 Controller 操作重定向到 Razor 页面:

return RedirectToPage("Edit", new { id = blogId });

我已经有一个名为“编辑”的 Razor 页面,当正常导航到它时它正在工作: enter image description here

使用 RedirectToPage 我得到以下错误:

InvalidOperationException: The relative page path 'Edit' 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.

知道如何指定该路径吗?

最佳答案

错误已经给了你答案:你应该在开头添加前导“/”并指定一个到你的 razor 页面的相对路径。所以你应该有

return RedirectToPage("/BlogPosts/Edit", new { id = blogId });

代替

return RedirectToPage("Edit", new { id = blogId });

请注意 "/BlogPosts/Edit""Edit" 之间的区别。 RedirectToPage 方法需要一个路径 到您的 razor 页面(根据您的图像,相对路径是 "/BlogPosts/Edit")从根开始文件夹,默认为 Pages

注意:从 Razor Pages 2.0.0 开始,redirecting to "sibling" pages works as well .换句话说,如果您在 /BlogPosts/View 有一个页面,它可以使用 RedirectToPage("Edit", new { id> 重定向到 /BlogPosts/Edit = blogId }),不指定根路径。

关于c# - ASP.net MVC core RedirectToPage错误-指定根相对路径错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51058793/

相关文章:

c# - 在对图像使用 UniformToFill 时,如何在 C# 或 XAML 中控制裁剪

c# - Request.CreateResponse 与response.Content?

c# - bool 类型的字段数组

ASP.NET Core 发布错误 : An error occurred while starting the application

c# - 如何在 asp.net core rc2 中获取 Controller 的自定义属性

c# - TryUpdateModelAsync() 失败但没有说明原因

c# - Enum 0 值不一致

c# - 尝试使用 Entity Framework 和迁移创建数据库时出现问题

c# - 哪里点razor文件夹 "_framework"和 "_content"?

asp.net-core - 如何添加所有 Razor 页面都可以访问的功能?