2011-04-11 143 views
0

我有以下问题。在我的解决方案中,我有两个控制器:家庭和帐户。一切工作到目前为止。但是,当我添加[授权]以上的HomeController如ASP.NET MVC 3部分视图未找到

[Authorize] 
public class HomeController : Controller 
{ 
... 

我得到

System.InvalidOperationException: The partial view 'Repository' was not found or no view engine supports the searched locations. The following locations were searched: 
~/Views/Account/Repository.aspx 
~/Views/Account/Repository.ascx 
~/Views/Shared/Repository.aspx 
~/Views/Shared/Repository.ascx 
~/Views/Account/Repository.cshtml 
~/Views/Account/Repository.vbhtml 
~/Views/Shared/Repository.cshtml 
~/Views/Shared/Repository.vbhtml 

局部视图仓库里面的HomeController所以因此它认为是在〜/浏览/首页/ Repository.cshtml,但它是分享在共享或帐户文件夹中。正如我所说,如果没有[Authorize]上面的HomeController类,一切都按预期工作。

发生错误的代码是在_Layout.cshtml

@Html.Partial("Repository") 

感谢您的帮助。

回答

9

_Layout.cshtml是整个应用程序的母版页。发生什么事是您的家庭控制器正在查找已通过身份验证的用户,发现该用户未通过身份验证并重定向到账户控制器中的登录操作。然后,该操作将呈现其视图,该视图使用_Layout.cshtml母版页来呈现局部视图,但无法找到它,因为它不在Account文件夹中。

简答:您的母版页中的部分视图应位于〜/ Views/Shared目录中。或者将该部分调用移动到只能在主控制器上下文中运行的页面。

+0

好吧我现在明白了,谢谢。 – user433947 2011-04-11 21:59:35

0

此外,请检查以下内容:确保输出类型设置为相关页面的内容。这是同行Joe F.向我指出的。无论出于何种原因,有时输出不能正确设置。因此,当您宣传您的应用程序时,不会复制cshtml文件。

0

当您在actionresult之上使用[Authorize]之类的属性时,这意味着特定View需要具有管理员安全权限或其他权限。这可以通过进入web.config并设置自动化来解决。例如

<location path="Admin/Repository.aspx"> 
    <system.web> 
     <authorization> 
     <allow roles="Administrators,content managers" /> 
     <deny users="*" /> 
     </authorization> 
    </system.web> 
    </location>