MVC 区域 - 未找到视图

发布于 2024-08-28 18:28:40 字数 1330 浏览 10 评论 0原文

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

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

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

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

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

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

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

我的区域有多个控制器,有很多视图。在这些控制器方法中对特定视图的任何调用,例如“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 

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

我在这里缺少什么?

提前致谢。

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.

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

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

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.

What am I missing here ?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

奶气 2024-09-04 18:28:40

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

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.

仲春光 2024-09-04 18:28:40

如果您使用而不是

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

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

您的

xyzAreaRegistration.cs

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

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

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

画中仙 2024-09-04 18:28:40

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

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

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").

[RouteArea("Xyz", AreaPrefix = "")]
public class XyzController : Controller   
{
...
}
滿滿的愛 2024-09-04 18:28:40

如果这是路由问题,您可以通过先注册您的区域路由来解决它。。这会导致路由引擎在匹配根路由之前尝试匹配区域路由之一:

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

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

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

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

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, before matching 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.

笑梦风尘 2024-09-04 18:28:40

检查 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 }
        );
    }

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

public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "SomeArea_default",
            "SomeArea/{controller}/{action}/{id}",
            new { controller = "SomeController", action = "Index", id = UrlParameter.Optional }
        );
    }
伏妖词 2024-09-04 18:28:40

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

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

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

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?}");
        });

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

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

}

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

 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();
    }
}

}

羁绊已千年 2024-09-04 18:28:40

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

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

天冷不及心凉 2024-09-04 18:28:40

试试这个代码。更改区域注册文件...

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" }
);

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" }
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文