我使用 ASP.NET MVC3 框架创建了一个 Web 应用程序。在项目中,我添加了名为 Administrator 的新区域,其中包含 HomeController 和 Index 操作。
它与 http://localhost:2813/Administrator/Home/Index
但是当我尝试使用 URL 访问时 http://localhost:2813/Home/Index
我收到一条错误消息:
Server Error in '/' Application.
The view 'Index' or its master was not found or no view engine supports the searched > > > locations. The following locations were searched: ~/Views/Home/Index.aspx ~/Views/Home/Index.ascx ~/Views/Shared/Index.aspx ~/Views/Shared/Index.ascx ~/Views/Home/Index.cshtml ~/Views/Home/Index.vbhtml ~/Views/Shared/Index.cshtml ~/Views/Shared/Index.vbhtml
Description: An unhandled exception occurred during the execution of the current web > > > request. Please review the stack trace for more information about the error and where it > originated in the code.
我还在 maproute 中添加了命名空间,但它仍然不起作用。 有人有想法吗?谢谢。
更新问题:
这是 Global.ascx 中的 MapRoute routes.MapRoute( “默认”, "{controller}/{action}/{id}", new { controller = "Default", action = "Index", id = UrlParameter.Optional }, new[] { “SampleWebsite.Controllers” } );
这是 AdministratorAreaRegistration.ascx context.MapRoute( “行政人员”, "管理员/{controller}/{action}/{id}", new { area = "Administrator", controller = "Dashboard", action = "Index", id = UrlParameter.Optional }, new[] { "SampleWebsite.Areas.Administrator.Controllers"} );
编辑
如果我将 MapRoute 从 Global 移动到 AdministratorAreaRegistration ,问题就解决了(我无法理解和解释它)
context.MapRoute( “行政人员”, "管理员/{controller}/{action}/{id}", new { area = "Administrator", controller = "Dashboard", action = "Index", id = UrlParameter.Optional }, new[] { "SampleWebsite.Areas.Administrator.Controllers"} );
context.MapRoute( "Default",//路由名称 "{controller}/{action}/{id}",//带参数的 URL new { area = "", controller = "Home", action = "Index", id = UrlParameter.Optional },//参数默认值 命名空间:new[] { “SampleWebsite.Controllers” } );
现在,所有对 localhost/Home/Index 的请求都将返回 404 not found。只允许使用准确的区域名称(管理员)进行请求。
谢谢你的帮助!
请您参考如下方法:
当您使用 url /Home/Index 时,您不会经过该区域。在您的路由中定义了一个带有 Administrator 前缀的区域,该前缀在 url 中的存在允许路由引擎识别您在该区域内。
有一些possible workarounds有一个默认区域。
