【发布时间】:2014-04-25 10:54:11
【问题描述】:
我在我的应用程序中添加了一个区域。它被称为“示例”。有一个默认路由:
context.MapRoute(
"Examples_default",
"Examples/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
如果我有一个名为“MyExampleController.cs”的控制器和一个默认放置在 Views/MyExample/Index.cshtml 下的视图,这可以正常工作。
我可以到达网址"http://localhost/Examples/MyExample/Index"。
我想要的只是在 Views 下创建一个子文件夹,以增加一层组织。 IE。在 Views 下创建一个文件夹“NewExamples”并将 MyExample 移到那里。所以我可能有以下结构
Views/NewExamples/MyExample/Index.cshtml
Views/OldExamples/Example123/Index.cshtml
等等
我想访问一个网址"http://localhost/Examples/NewExamples/MyExample/Index"。
我正在努力寻找路线。我的方法是在默认路由之前添加一条路由:
context.MapRoute(
"Examples_newexamples",
"Examples/NewExamples/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
但这不起作用。我点击了控制器,但随后出现服务器错误
未找到视图“索引”或其主视图,或者没有视图引擎支持搜索到的位置。搜索了以下位置:
~/Areas/Examples/Views/MyExample/Index.aspx
~/Areas/Examples/Views/MyExample/Index.ascx
~/Areas/Examples/Views/Shared/Index.aspx
~/Areas/Examples/Views/Shared/Index.ascx
...
~/Areas/Examples/Views/MyExample/Index.cshtml
~/Areas/Examples/Views/MyExample/Index.vbhtml
~/Areas/Examples/Views/Shared/Index.cshtml
~/Areas/Examples/Views/Shared/Index.vbhtml
...
因此它甚至不会尝试查看 NewExamples 子文件夹。
是否存在包含此子文件夹的正确路径?
【问题讨论】:
-
为什么不简单地
return View("~/Areas/Examples/Views/NewExamples/MyExample/Index.cshtml");? -
谢谢,这行得通。如果您作为答案发布,将接受。 :) 只是希望通过路由作为更“优雅”的解决方案来实现同样的目标。
标签: asp.net-mvc routing asp.net-mvc-areas