MVC 区域 - 未找到视图
我有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
解决了!我的几个“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.
如果您使用而不是
在
您的
那么您不需要在任何链接中明确指定您的区域...
If you use instead of
use
in your
then you don't need to explicitly specify your area in any link...
在 Controller 类上添加 RouteArea 属性,以便 MVC 知道使用“XYZ”区域作为视图(然后您可以将 AreaPrefix 设置为空字符串,以便路由不需要以“XYZ”开头)。
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").
如果这是路由问题,您可以通过先注册您的区域路由来解决它。。这会导致路由引擎在匹配根路由之前尝试匹配区域路由之一:
如果我通过重命名区域应用程序中的视图文件夹之一来强制发生错误,则会收到与以下错误不同的错误:你的:
..如果它认为它在一个区域中,它会搜索子目录的模式。
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:
If I force an error by renaming one of my views folders in my areas application, I get a different error than yours:
..which is the pattern of subdirectories it would search if it thought it was in an area.
检查 MyAreaAreaRegistration.cs 中生成的代码,并确保控制器参数设置为您的默认控制器,否则控制器将由于某种原因被称为 bot ASP.NET MVC 不会搜索区域文件夹中的视图
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
对于那些正在寻找 .net core 解决方案的人,请使用
如果您在主项目中有一些代码并且在区域中有一些代码,请使用以下代码。
请注意,确保您的控制器中定义了区域
}
For those who are looking for .net core solution please use
If you have some code in main project and some code in areas use the following code.
Note make sure you have areas defined in your controller
}
我刚刚遇到了同样的问题,并通过将 ascx 的“构建操作”属性设置为“嵌入资源”来解决它。
I just had the same problem and solved it by setting the ascx's 'Build Action' property to 'Embedded Resource'.
试试这个代码。更改区域注册文件...
Try this code. Do changes in Area Registration File...