gpt4 book ai didi

c# - Asp.NET MVC3 - URL 中没有区域的访问区域 Controller

转载 作者:太空宇宙 更新时间:2023-11-03 21:59:11 25 4
gpt4 key购买 nike

我使用 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.ascxcontext.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有一个默认区域。

关于c# - Asp.NET MVC3 - URL 中没有区域的访问区域 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10970679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com