I created a Area in my MVC 3 application called 'Blog'.
我在MVC 3应用程序中创建了一个名为“Blog”的区域。
In global.asax I have the following code.
在全球。asax我有以下代码。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
This is the code of my Area
这是我所在地区的代码
public class BlogAreaRegistration : AreaRegistration
{
public override string AreaName
{
get { return "Blog"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Blog_default",
"Blog/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
When I go to the following url http://localhost/CMS/blog I get the following error.
当我转到下面的url http://localhost/cms/blog时,我得到以下错误。
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/blog/Index.aspx ~/Views/blog/Index.ascx ~/Views/Shared/Index.aspx ~/Views/Shared/Index.ascx ~/Views/blog/Index.cshtml ~/Views/blog/Index.vbhtml ~/Views/Shared/Index.cshtml ~/Views/Shared/Index.vbhtml
没有找到视图“索引”或它的主目录,或者没有视图引擎支持搜索位置。搜索了以下位置:~/ view /blog/Index。aspx ~ /视图/博客/索引。ascx ~ /视图/共享/索引。aspx ~ /视图/共享/索引。ascx ~ /视图/博客/索引。cshtml ~ /视图/博客/索引。vbhtml ~ /视图/共享/索引。cshtml ~ / /共享/ Index.vbhtml观点
How do I solve this?
怎么解呢?
8 个解决方案
#1
8
The registration in your area appears to be wrong. You specify a default for your action but not for the controller. Since you typically have Home as the name of the controller you'd need to specify that.
你所在地区的注册似乎是错误的。为操作指定默认值,但不为控制器指定默认值。由于通常将Home作为控制器的名称,所以需要指定它。
Also it could be you don't have your folders setup correctly since you should have physically setup:
也可能你没有正确设置文件夹,因为你应该有物理设置:
- /Areas/Blog
- /地区/博客
- /Areas/Blog/Controllers
- /地区/博客/控制器
- /Areas/Blog/Views
- /地区/博客/观点
... and once you have fixed your blog area route you'll also need:
…一旦你确定了你的博客区域路线,你也需要:
- /Areas/Blog/Views/Home << Put your index view in here
- <把你的索引视图放在这里< li>
The error you get seems to pretty clearly indicate this is the issue.
你得到的错误似乎很清楚地表明这就是问题所在。
#2
26
I found what I consider to be a bug in the framework, with a workaround. If you are trying to map a default route to an MVC 3 app with areas, your global.asax file might have something like this:
我在框架中找到了一个我认为是bug的东西,并找到了解决方案。如果你想要将默认路径映射到MVC 3应用中,你的全局。asax文件可能有以下内容:
VB:
VB:
routes.MapRoute(
"Default",
"{area}/{controller}/{action}/{id}",
New With {.area = "MyArea", .controller = "Home", .action = "Index", .id = UrlParameter.Optional}
)
C#:
c#:
routes.MapRoute(
"Default",
"{area}/{controller}/{action}/{id}",
new { area = "MyArea", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
If you go to your app root in the URL, you may get a runtime error like this:
如果你在URL中找到你的应用程序根,你可能会得到如下的运行时错误:
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
没有找到视图“索引”或它的主目录,或者没有视图引擎支持搜索位置。搜查了下列地点:
For some reason, the view engine does not appear to look in the area folder for the view file the same as if you type in the whole link. The strange thing is the code reaches the controller action. Here is the fix: Put this code in your controller action:
由于某些原因,视图引擎在区域文件夹中查找视图文件的方式与在整个链接中输入的方式不同。奇怪的是代码到达控制器动作。解决方法是:将此代码放到控制器动作中:
VB:
VB:
If Not Me.ControllerContext.RouteData.DataTokens.ContainsKey("area") Then
Me.ControllerContext.RouteData.DataTokens.Add("area", "MyArea")
End If
C#
c#
if (!this.ControllerContext.RouteData.DataTokens.ContainsKey("area"))
{
this.ControllerContext.RouteData.DataTokens.Add("area", "MyArea")
}
#3
3
I'm using Phil Haack's routedebugger to troubleshoot problems such as this one. It conveniently shows all registered routes and how the entered URL matches your configuration.
我正在使用Phil Haack的routedebugger来排除诸如此类的问题。它方便地显示所有已注册的路由以及输入的URL如何匹配您的配置。
It works by overriding the regular application flow, which you enable by adding this line at the end of Application_Start:
它通过覆盖常规的应用程序流来工作,您可以在Application_Start的末尾添加这一行:
RouteDebug.RouteDebugger.RewriteRoutesForTesting( RouteTable.Routes );
#4
3
Relax! This can save you hours of reading, make a coffee and watch this 3 minutes video, everything will be clear to you. http://www.asp.net/mvc/videos/mvc-2/how-do-i/aspnet-mvc-2-areas (I belive it also works for mvc3, mvc4, and mvc2035)
放松点!这可以帮你省下几个小时的阅读时间,泡杯咖啡,看这个3分钟的视频,一切都会变得清晰起来。http://www.asp.net/mvc/videos/mvc-2/how-do-i/aspnet-mvc-2个区域(我认为它也适用于mvc3、mvc4和mvc2035)
#5
2
Following on from Mortens answer, you can now NuGet (or manually download and install) the RouteMagic, or Glimpse packages which provides this functionality, and more.
根据Mortens的回答,您现在可以直接获取(或手动下载和安装)RouteMagic,或者瞥见提供此功能的包,等等。
More information is available on Phil Haacks blog regarding the state of his tool and what it has morphed into. The comments make for good reading too!
更多的信息可以在Phil Haacks博客上找到,关于他的工具的状态和它演变成什么。这些评论也有助于阅读!
#6
0
Check if you have a virtual path is mapped as the area name. I set area address in visual studio just to debug and it asked me to create a virtual path. So AppRelativeCurrentExecutionFilePath was always ~/ and routing wasn't able to determine the area. For IIS express delete the virtual path for your site:
检查是否将虚拟路径映射为区域名称。我在visual studio中设置了区域地址以便调试,它要求我创建一个虚拟路径。因此,对于该区域,我们总是无法确定它的位置。对于IIS express,删除站点的虚拟路径:
C:\Users\username\Documents\IISExpress\config\applicationhost.config
I spent 4 days to discover it.
我花了4天时间才发现它。
#7
0
I had the same issue as Mindstorm Interactive, and took a slightly different approach. Yes, it does redirect the user, but it made my code look less workaroundy so to speak.
我和Mindstorm Interactive有同样的问题,并且采用了一种稍微不同的方法。是的,它确实重定向了用户,但是可以说,它使我的代码看起来不那么复杂。
I created a new controller, no matter in what area, since the problem is within the renderer not finding the view, not the controller.
我创建了一个新的控制器,不管它位于哪个区域,因为问题是在渲染器中,而不是控制器,没有找到视图。
The controller then redirects with the area (as in Mindstroms fix, the key) included, and voila.
然后控制器重新定向到该区域(就像在Mindstroms修复中一样),然后是voila。
public class StartController : Controller
{
public ActionResult Index()
{
return RedirectToAction("Index", "MyController", new { area = "MyArea" });
}
}
in the RouteConfig just add
在RouteConfig中添加。
routes.MapRoute(name: "Root", url: "", defaults: new { controller = "Start", action = "Index", area = "MyArea" });
Hope it helps someone.
希望它能帮助一些人。
#8
0
Just try to delete content from following directories as mentioned here and rebuild project
尝试从下面的目录中删除内容并重新构建项目
C:\Temp C:\Users\%Username%\AppData\Local\Microsoft\VisualStudio C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files Path\To\Your\Project\obj\Debug
C:\ Temp C:\Users\ %用户名% \ AppData \当地\微软\ VisualStudio C:\Windows\ Microsoft.NET \ Framework \ v4.0.30319 \临时ASP。网络文件C:\Windows\ Microsoft.NET \ Framework64 \ v4.0.30319 \临时ASP。网络文件路径\ \ \项目\ obj \调试
#1
8
The registration in your area appears to be wrong. You specify a default for your action but not for the controller. Since you typically have Home as the name of the controller you'd need to specify that.
你所在地区的注册似乎是错误的。为操作指定默认值,但不为控制器指定默认值。由于通常将Home作为控制器的名称,所以需要指定它。
Also it could be you don't have your folders setup correctly since you should have physically setup:
也可能你没有正确设置文件夹,因为你应该有物理设置:
- /Areas/Blog
- /地区/博客
- /Areas/Blog/Controllers
- /地区/博客/控制器
- /Areas/Blog/Views
- /地区/博客/观点
... and once you have fixed your blog area route you'll also need:
…一旦你确定了你的博客区域路线,你也需要:
- /Areas/Blog/Views/Home << Put your index view in here
- <把你的索引视图放在这里< li>
The error you get seems to pretty clearly indicate this is the issue.
你得到的错误似乎很清楚地表明这就是问题所在。
#2
26
I found what I consider to be a bug in the framework, with a workaround. If you are trying to map a default route to an MVC 3 app with areas, your global.asax file might have something like this:
我在框架中找到了一个我认为是bug的东西,并找到了解决方案。如果你想要将默认路径映射到MVC 3应用中,你的全局。asax文件可能有以下内容:
VB:
VB:
routes.MapRoute(
"Default",
"{area}/{controller}/{action}/{id}",
New With {.area = "MyArea", .controller = "Home", .action = "Index", .id = UrlParameter.Optional}
)
C#:
c#:
routes.MapRoute(
"Default",
"{area}/{controller}/{action}/{id}",
new { area = "MyArea", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
If you go to your app root in the URL, you may get a runtime error like this:
如果你在URL中找到你的应用程序根,你可能会得到如下的运行时错误:
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
没有找到视图“索引”或它的主目录,或者没有视图引擎支持搜索位置。搜查了下列地点:
For some reason, the view engine does not appear to look in the area folder for the view file the same as if you type in the whole link. The strange thing is the code reaches the controller action. Here is the fix: Put this code in your controller action:
由于某些原因,视图引擎在区域文件夹中查找视图文件的方式与在整个链接中输入的方式不同。奇怪的是代码到达控制器动作。解决方法是:将此代码放到控制器动作中:
VB:
VB:
If Not Me.ControllerContext.RouteData.DataTokens.ContainsKey("area") Then
Me.ControllerContext.RouteData.DataTokens.Add("area", "MyArea")
End If
C#
c#
if (!this.ControllerContext.RouteData.DataTokens.ContainsKey("area"))
{
this.ControllerContext.RouteData.DataTokens.Add("area", "MyArea")
}
#3
3
I'm using Phil Haack's routedebugger to troubleshoot problems such as this one. It conveniently shows all registered routes and how the entered URL matches your configuration.
我正在使用Phil Haack的routedebugger来排除诸如此类的问题。它方便地显示所有已注册的路由以及输入的URL如何匹配您的配置。
It works by overriding the regular application flow, which you enable by adding this line at the end of Application_Start:
它通过覆盖常规的应用程序流来工作,您可以在Application_Start的末尾添加这一行:
RouteDebug.RouteDebugger.RewriteRoutesForTesting( RouteTable.Routes );
#4
3
Relax! This can save you hours of reading, make a coffee and watch this 3 minutes video, everything will be clear to you. http://www.asp.net/mvc/videos/mvc-2/how-do-i/aspnet-mvc-2-areas (I belive it also works for mvc3, mvc4, and mvc2035)
放松点!这可以帮你省下几个小时的阅读时间,泡杯咖啡,看这个3分钟的视频,一切都会变得清晰起来。http://www.asp.net/mvc/videos/mvc-2/how-do-i/aspnet-mvc-2个区域(我认为它也适用于mvc3、mvc4和mvc2035)
#5
2
Following on from Mortens answer, you can now NuGet (or manually download and install) the RouteMagic, or Glimpse packages which provides this functionality, and more.
根据Mortens的回答,您现在可以直接获取(或手动下载和安装)RouteMagic,或者瞥见提供此功能的包,等等。
More information is available on Phil Haacks blog regarding the state of his tool and what it has morphed into. The comments make for good reading too!
更多的信息可以在Phil Haacks博客上找到,关于他的工具的状态和它演变成什么。这些评论也有助于阅读!
#6
0
Check if you have a virtual path is mapped as the area name. I set area address in visual studio just to debug and it asked me to create a virtual path. So AppRelativeCurrentExecutionFilePath was always ~/ and routing wasn't able to determine the area. For IIS express delete the virtual path for your site:
检查是否将虚拟路径映射为区域名称。我在visual studio中设置了区域地址以便调试,它要求我创建一个虚拟路径。因此,对于该区域,我们总是无法确定它的位置。对于IIS express,删除站点的虚拟路径:
C:\Users\username\Documents\IISExpress\config\applicationhost.config
I spent 4 days to discover it.
我花了4天时间才发现它。
#7
0
I had the same issue as Mindstorm Interactive, and took a slightly different approach. Yes, it does redirect the user, but it made my code look less workaroundy so to speak.
我和Mindstorm Interactive有同样的问题,并且采用了一种稍微不同的方法。是的,它确实重定向了用户,但是可以说,它使我的代码看起来不那么复杂。
I created a new controller, no matter in what area, since the problem is within the renderer not finding the view, not the controller.
我创建了一个新的控制器,不管它位于哪个区域,因为问题是在渲染器中,而不是控制器,没有找到视图。
The controller then redirects with the area (as in Mindstroms fix, the key) included, and voila.
然后控制器重新定向到该区域(就像在Mindstroms修复中一样),然后是voila。
public class StartController : Controller
{
public ActionResult Index()
{
return RedirectToAction("Index", "MyController", new { area = "MyArea" });
}
}
in the RouteConfig just add
在RouteConfig中添加。
routes.MapRoute(name: "Root", url: "", defaults: new { controller = "Start", action = "Index", area = "MyArea" });
Hope it helps someone.
希望它能帮助一些人。
#8
0
Just try to delete content from following directories as mentioned here and rebuild project
尝试从下面的目录中删除内容并重新构建项目
C:\Temp C:\Users\%Username%\AppData\Local\Microsoft\VisualStudio C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files Path\To\Your\Project\obj\Debug
C:\ Temp C:\Users\ %用户名% \ AppData \当地\微软\ VisualStudio C:\Windows\ Microsoft.NET \ Framework \ v4.0.30319 \临时ASP。网络文件C:\Windows\ Microsoft.NET \ Framework64 \ v4.0.30319 \临时ASP。网络文件路径\ \ \项目\ obj \调试