Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自定义RouteBase中视图未拾取的区域_C#_Asp.net Mvc_Asp.net Mvc 5 - Fatal编程技术网

C# 自定义RouteBase中视图未拾取的区域

C# 自定义RouteBase中视图未拾取的区域,c#,asp.net-mvc,asp.net-mvc-5,C#,Asp.net Mvc,Asp.net Mvc 5,我有一个自定义的RouteBase,MyRoute,我想为一个区域“MyArea”工作,该区域包含如下代码: public override GetRouteData(HttpContextBase httpContext) { var result = new RouteData(this, new MvcRouteHandler()); result.Values.Add("area", "MyArea"); result.Values.Add("controlle

我有一个自定义的
RouteBase
MyRoute
,我想为一个区域
“MyArea”
工作,该区域包含如下代码:

public override GetRouteData(HttpContextBase httpContext)
{
    var result = new RouteData(this, new MvcRouteHandler());

    result.Values.Add("area", "MyArea");
    result.Values.Add("controller", "MyController");
    result.Values.Add("action", "Index");
}
我在
myAreaRegistration.cs
文件中注册此项:

public override string AreaName { get { return "MyArea"; } }

public override void RegisterArea(AreaRegistrationContext context)
{
    context.Routes.Add(new MyRoute());

    // other routes
    context.MapRoute(/* ... */);
}
当发出请求时,它成功地调用
MyController上的
索引
操作:

public ActionResult Index()
{
    return this.View();
}
但是,MVC不会在正确的文件夹中搜索视图:

找不到视图“索引”或其主视图,或者没有视图引擎支持搜索的位置。已搜索以下位置:
~/Views/MyController/Index.aspx
~/Views/MyController/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/MyController/Index.cshtml
~/Views/MyController/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml

当视图位于中时

~/Areas/MyArea/Views/MyController/Index.cshtml

如何在正确的区域进行MVC搜索?

如果查看
AreaRegistrationContext.MapRoute
的源代码,您可以看到它将该区域与其他路由变量区别对待:

public Route MapRoute(string name, string url, object defaults, object constraints, string[] namespaces)
{
    if (namespaces == null && this.Namespaces != null)
    {
        namespaces = this.Namespaces.ToArray<string>();
    }
    Route route = this.Routes.MapRoute(name, url, defaults, constraints, namespaces);
    route.DataTokens["area"] = this.AreaName; // *** HERE! ***
    bool flag = namespaces == null || namespaces.Length == 0;
    route.DataTokens["UseNamespaceFallback"] = flag;
    return route;
}

result.Values.Add("area", "MyArea");
result.DataTokens["area"] = "MyArea";