asp.net-mvc MVC 区域 - 未找到视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2624566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:15:39  来源:igfitidea点击:

MVC Areas - View not found

asp.net-mvcasp.net-mvc-areas

提问by user314827

I have a project that is using MVC areas. The area has the entire project in it while the main "Views/Controllers/Models" folders outside the Areas are empty barring a dispatch controller I have setup that routes default incoming requests to the Home Controller in my area.

我有一个使用 MVC 区域的项目。该区域包含整个项目,而区域外的主要“视图/控制器/模型”文件夹是空的,除非我设置了将默认传入请求路由到我所在区域的主控制器的调度控制器。

This controller has one method as follows:-

该控制器有一种方法如下:-

public ActionResult Index(string id)
    {
        return RedirectToAction("Index", "Home", new {area = "xyz"});
    }   

I also have a default route setup to use this controller as follows:-

我还有一个默认路由设置来使用这个控制器,如下所示:-

routes.MapRoute(
            "Default",                                              // Default route
            "{controller}/{action}/{id}",
            new { controller = "Dispatch", action = "Index", id = UrlParameter.Optional }
        );   

Any default requests to my site are appropriately routed to the relevant area. The Area's "RegisterArea" method has a single route:-

对我网站的任何默认请求都会适当地路由到相关区域。Area 的“RegisterArea”方法有一条路线:-

context.MapRoute(
            "xyz_default",
            "xyz/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }

My area has multiple controllers with a lot of views. Any call to a specific view in these controller methods like "return View("blah"); renders the correct view. However whenever I try and return a view along with a model object passed in as a parameter I get the following error:-

我的区域有多个控制器,有很多视图。在这些控制器方法中对特定视图的任何调用,例如“return View(”blah”); 都会呈现正确的视图。但是,每当我尝试返回视图以及作为参数传入的模型对象时,我都会收到以下错误:-

Server Error in '/DeveloperPortal' Application.
The view 'blah' or its master was not found. The following locations were searched:
~/Views/Profile/blah.aspx
~/Views/Profile/blah.ascx
~/Views/Shared/blah.aspx
~/Views/Shared/blah.ascx 

It looks like whenever a model object is passed in as a param. to the "View()" method [e.g. return View("blah",obj) ] it searches for the view in the root of the project instead of in the area specific view folder.

看起来就像每当模型对象作为参数传入时。到“View()”方法[例如 return View("blah",obj) ] 它在项目的根目录中而不是在特定于区域的视图文件夹中搜索视图。

What am I missing here ?

我在这里错过了什么?

Thanks in advance.

提前致谢。

回答by user314827

Solved ! A couple of my "RedirectToAction" calls were not specifying the area name explicitly in the routeobject collection parameter of that method. Weird though, that that is required even though the controllers Redirecting are all in the same area. Also, the HtmlActionLinks work fine when I don't specify the new {area="blah"} in its routeobject collection, so I wonder why the controller action calls to RedirectToAction() need that even though both the calling and the called controller actions are all within the same area.

解决了 !我的几个“RedirectToAction”调用没有在该方法的 routeobject 集合参数中明确指定区域名称。奇怪的是,即使控制器重定向都在同一区域,这也是必需的。此外,HtmlActionLinks 在我没有在它的 routeobject 集合中指定新的 {area="blah"} 时工作正常,所以我想知道为什么控制器动作调用 RedirectToAction() 需要它,即使调用和被调用的控制器动作都在同一个区域内。

回答by Morgli

If you use instead of

如果您使用代替

context.MapRoute(
        "xyz_default",
        "xyz/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional }

use

context.MapRoute(
        "xyz_default",
        "{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional }

in your

在你的

xyzAreaRegistration.cs

xyzAreaRegistration.cs

then you don't need to explicitly specify your area in any link...

那么你不需要在任何链接中明确指定你的区域......

回答by Jeff Widmer

Add the RouteArea attribute on the Controller class so MVC knows to use the "XYZ" Area for the views (and then you can set the AreaPrefix to empty string so routes do not need to start with "XYZ").

在 Controller 类上添加 RouteArea 属性,以便 MVC 知道对视图使用“XYZ”区域(然后您可以将 AreaPrefix 设置为空字符串,这样路由就不需要以“XYZ”开头)。

[RouteArea("Xyz", AreaPrefix = "")]
public class XyzController : Controller   
{
...
}

回答by Robert Harvey

If this is a routing problem, you can fix it by registering your area routes first. This causes the routing engine to try matching one of the area routes, beforematching a root route:

如果这是一个路由的问题,你可以通过注册你的区域内路由修复它第一。这会导致路由引擎匹配根路由之前尝试匹配区域路由之一:

AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);

If I force an error by renaming one of my views folders in my areas application, I get a different error than yours:

如果我通过在我的区域应用程序中重命名我的视图文件夹之一来强制错误,我会收到与您不同的错误:

The view 'Index' or its master was not found. The following locations 
  were searched:

~/Areas/xyz/Views/Document/Index.aspx
~/Areas/xyz/Views/Document/Index.ascx
~/Areas/xyz/Views/Shared/Index.aspx
~/Areas/xyz/Views/Shared/Index.ascx

...and then the usual root view folders.. 

..which is the pattern of subdirectories it would search if it thought it was in an area.

..如果它认为它在一个区域中,它会搜索子目录的模式。

回答by Raphael

Check the generated code at MyAreaAreaRegistration.cs and make sure that the controller parameter is set to your default controller, otherwise the controller will be called bot for some reason ASP.NET MVC won't search for the views at the area folder

检查MyAreaAreaRegistration.cs 中生成的代码并确保控制器参数设置为您的默认控制器,否则控制器将因某种原因被称为 bot ASP.NET MVC 不会在区域文件夹中搜索视图

public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "SomeArea_default",
            "SomeArea/{controller}/{action}/{id}",
            new { controller = "SomeController", action = "Index", id = UrlParameter.Optional }
        );
    }

回答by John MacIntyre

I just had the same problem and solved it by setting the ascx's 'Build Action' property to 'Embedded Resource'.

我刚刚遇到了同样的问题,并通过将 ascx 的“构建操作”属性设置为“嵌入式资源”来解决它。

回答by rav

Try this code. Do changes in Area Registration File...

试试这个代码。在区域注册文件中进行更改...

context.MapRoute(
    "YourRouteName",   // Route name //
    "MyAreaName/MyController/{action}",   // URL with parameters //
    new { 
        controller = "MyControllerName", 
        action = "MyActionName", meetId =  UrlParameter.Optional
     },   // Parameter defaults
    new[] { "Your Namespace name" }
);

回答by tksdotnet1

For those who are looking for .net core solution please use

对于那些正在寻找 .net 核心解决方案的人,请使用

 app.UseMvc(routes =>
    {
      routes.MapRoute(
        name : "areas",
        template : "{area:exists}/{controller=Home}/{action=Index}/{id?}"
      );
    });`

If you have some code in main project and some code in areas use the following code.

如果您在主项目中有一些代码而在区域中有一些代码,请使用以下代码。

app.UseMvc(routes => {
            routes.MapRoute(
                name: "areas",
                template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");

            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

Note make sure you have areas defined in your controller

请注意确保您在控制器中定义了区域

 [Area("Test")]
public class TestController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

}

}