ASP.net MVC核心RedirectToPage错误-指定根相对路径错误 [英] ASP.net MVC core RedirectToPage error - specify root relative path error

查看:460
本文介绍了ASP.net MVC核心RedirectToPage错误-指定根相对路径错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从正常的控制器操作重定向到剃刀页面,如下所示:

I want to redirect to a razor page from a normal controller action like this:

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

我已经有一个名为"Edit"的剃须刀页面,该页面在正常导航至该页面时可以正常工作:

I have already a razor page named "Edit" which is working when navigating to it normally:

使用RedirectToPage时出现以下错误:

InvalidOperationException:相对页面路径编辑"只能是 在执行剃刀页面"时使用.用一个指定根相对路径 前导"/"以在Razor Page之外生成URL.

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.

有什么想法要指定该路径吗?

Any idea how to specify that path?

推荐答案

该错误已经为您提供了答案:您应该在开头添加"/",并指定到剃刀页面的相对路径.所以你应该有

The error already gave you the answer: You should add the leading '/' at the start and specify a relative path to your razor page. So you should have

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

代替

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

请注意"/BlogPosts/Edit" "Edit" 之间的区别. RedirectToPage方法期望到剃刀页面的路径(基于图像,相对路径为"/BlogPosts/Edit" ),从根文件夹默认情况下.

Notice the difference between "/BlogPosts/Edit" and "Edit". RedirectToPage method expects a path to your razor page (based on your image the relative path is "/BlogPosts/Edit") starting to the root folder which is Pages by default.

注意:从Razor Pages 2.0.0开始,重定向到兄弟"页面也可以正常工作.换句话说,如果您在/BlogPosts/View处有一个页面,则可以使用RedirectToPage("Edit", new { id = blogId })重定向到/BlogPosts/Edit,而无需指定根目录路径.

Note: Starting with Razor Pages 2.0.0, redirecting to "sibling" pages works as well. In other words, if you have a page at /BlogPosts/View, it could redirect to /BlogPosts/Edit with RedirectToPage("Edit", new { id = blogId }), without specifying a rooted path.

这篇关于ASP.net MVC核心RedirectToPage错误-指定根相对路径错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆