gpt4 book ai didi

c# - 区域内的mvc5属性路由找不到 View

转载 作者:可可西里 更新时间:2023-11-01 07:44:04 25 4
gpt4 key购买 nike

当我在 Admin 区域内并使用属性路由映射我的路线时,它找不到 View ,因为它不在实际区域 View 文件夹中查找,而只在全局 View 文件夹中查找。

只有我通过完整路径查看它才能显示它,否则它会抛出错误。

错误

The view 'Authorize' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Authorize.aspx
~/Views/Home/Authorize.ascx
~/Views/Shared/Authorize.aspx
~/Views/Shared/Authorize.ascx
~/Views/Home/Authorize.cshtml
~/Views/Home/Authorize.vbhtml
~/Views/Shared/Authorize.cshtml
~/Views/Shared/Authorize.vbhtml

代码

[RoutePrefix("admin")]
public class HomeController : Controller
{

[Route]
public ActionResult Index()
{
return View("Authorize"); // Error
return View("~/Areas/Admin/Views/Home/Authorize.cshtml"); // Working
}
}

请注意,如果我禁用属性路由并切换回良好的旧路由,它将起作用。有什么方法可以解决这个问题,或者它按预期工作,我应该在我的所有区域应用完整路径吗?

最佳答案

您需要将 [RouteArea("")] 属性添加到您的 Controller :

[RouteArea("Admin")]
public class HomeController : Controller

您可以找到文档 here .

关于c# - 区域内的mvc5属性路由找不到 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20516390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com