编程语言
首页 > 编程语言> > c#-使用MVC 5 RouteArea属性时找不到默认区域视图

c#-使用MVC 5 RouteArea属性时找不到默认区域视图

作者:互联网

我有一个涉及多个领域的MVC5项目.我有一个默认区域(名为Default),其中有一个默认控制器(名为DefaultController).这可以在站点路线上访问.

[RouteArea]
public class DefaultController : Controller
{
    [Route]
    public ActionResult Index()
    {
        return View("Index");
    }
}

public static void RegisterRoutes(RouteCollection routes)
{
    routes.LowercaseUrls = true;

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapMvcAttributeRoutes();

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional },
        namespaces: new[] { "MyProject.Areas.Default.Controllers" }
    );
}

控制器已正确加载,但是找不到视图(位于Areas / Default / Views / Default / Index.cshtml).为什么MVC找不到正确的位置?

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

解决方法:

我看到两个选择:

>显式指定并使用[RouteArea(“ Default”,AreaPrefix =“”)]指定areaName,其中“ Default”是您区域的名称,设置AreaPrefix =“”将在应用程序的根目录中获取区域.
>更改控制器所在的名称空间,以使最后一部分与您所在区域的名称匹配.

请注意,如果您区域的前缀是根(“”),则需要使用操作[Route(“ …”)]属性非常明确,以免它们贪婪地声明应用程序中的所有路由,并且出于种种其他原因,让您挠头.

背景

如果在未提供areaName的情况下应用RouteAreaAttribute或提供的areaName为null,则可能会出现此奇怪错误,或者您的操作可能使用了错误的视图(例如,如果您出于某些其他目的而创建〜/ Views / Shared / Index.cshtml ).

RouteAreaAttribute.AreaName文档很有帮助:

(The AreaName property) gets the area name to set for all the routes defined in the controller. If the value is null, an attempt will be made to infer the area name from the target controller’s namespace.

通过将[RouteArea]应用于控制器,areaName默认为null,这将导致某些逻辑接管,从而“从目标控制器的名称空间中推断区域名称”.显然,推断涉及获取名称空间的最后一部分,传统上该部分是“ Controllers”,因为已教会MVC开发人员在Controllers文件夹中创建其控制器,并且默认情况下,Visual Studio根据文件夹结构确定C#类的名称空间.

为了证明我的观点,您可以将DefaultController的命名空间从MyProject.Areas.Default.Controllers更改为MyProject.Areas.Default,并切断“ .Controllers”.如果您重建并访问该操作,它将像我们所有人所期望的那样在“ Areas / Default”文件夹下查找视图:

~/Areas/Default/Views/Default/Index.aspx
~/Areas/Default/Views/Default/Index.ascx
...

而不是没有人期望的在“区域/控制器”文件夹下:

~/Areas/Controllers/Views/Default/Index.aspx
~/Areas/Controllers/Views/Default/Index.ascx
...

我知道为什么开发人员选择以这种方式进行操作,但是不幸的是他们忽略了我们被教导如何组织ASP.NET MVC代码以及Visual Studio如何为项目添加区域和控制器的方法.这不完全是一个错误,而是比它更微妙和令人沮丧的东西.

标签:asp-net-mvc-5,attributerouting,asp-net-mvc-routing,c,asp-net-mvc
来源: https://codeday.me/bug/20191029/1962475.html