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

标签 c# asp.net-mvc asp.net-mvc-5 asp.net-mvc-areas attributerouting

当我在 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/

相关文章:

asp.net - 方法 'Label' 没有重载需要 3 个参数

javascript - 仅当值更改时才启用“提交”按钮

c# - ASP.Net MVC 表单发布无法绑定(bind)模型列表属性

c# - RegEx.Replace 仅替换第一次出现的情况,需要全部

asp.net-mvc - 在 MVC 5 上设置 Jasmine 测试框架

c# - RouteValueDictionary 和 htmlAttributes 之间是否存在冲突?

jquery - 如何在 Kendo UI 网格中获得行号

asp.net-mvc - 如何在 Visual Studio 中更改我的 asp.net mvc Web 应用程序的 ssl 端口号

c# - 在默认浏览器中运行selenium?

c# - 如何在 WinRT 应用程序中获取实际语言?