区域内的mvc5属性路由找不到视图

时间:2013-12-11 10:17:47

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

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

只有当我通过完整路径查看它然后才能显示它,否则会引发错误。

错误

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
    }
}

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

1 个答案:

答案 0 :(得分:24)

您需要将[RouteArea("")]属性添加到控制器:

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

您可以找到文档here