【问题标题】:Asp.NET MVC3 - Access Area's Controller Without Area in URLAsp.NET MVC3 - URL 中没有区域的访问区域控制器
【发布时间】:2012-06-13 19:14:31
【问题描述】:

我使用 ASP.NET MVC3 框架创建了一个 Web 应用程序。在项目中,我使用 HomeController 和 Index 操作添加了名为 Administrator 的新区域。

它适用于 http://localhost:2813/Administrator/Home/Index

但是当我尝试使用 URL 访问时 http://localhost:2813/Home/Index

我收到一条错误消息:

“/”应用程序中的服务器错误。

未找到视图“索引”或其主视图,或者没有视图引擎支持搜索的 > > > 位置。搜索了以下位置: ~/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

说明:在执行当前web>>>请求时发生了一个未处理的异常。请查看堆栈跟踪以获取有关错误的更多信息以及它 > 源自代码的位置。

我还在 maproute 中添加了命名空间,但它仍然无法正常工作。 有人有想法吗?谢谢。

更新问题:

这是 Global.ascx 中的 MapRoute routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Default", action = "Index", id = UrlParameter.Optional }, new[] { "SampleWebsite.Controllers" } );

这是 AdministratorAreaRegistration.ascx context.MapRoute( "Administrator", "Administrator/{controller}/{action}/{id}", new { area = "Administrator", controller = "Dashboard", action = "Index", id = UrlParameter.Optional }, new[] { "SampleWebsite.Areas.Administrator.Controllers" } );

编辑
如果我将 MapRoute 从 Global 移动到 AdministratorAreaRegistration,问题就解决了(我无法理解和解释它)
context.MapRoute( "Administrator", "Administrator/{controller}/{action}/{id}", new { area = "Administrator", controller = "Dashboard", action = "Index", id = UrlParameter.Optional }, new[] { "SampleWebsite.Areas.Administrator.Controllers" } );
context.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { area = "", controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults namespaces: new[] { "SampleWebsite.Controllers" } );

现在,对localhost/Home/Index 的所有请求都将是未找到响应 404。只允许具有确切区域名称(管理员)的请求。

感谢您的帮助!

【问题讨论】:

  • 我们可以查看您的路线定义吗? (MapRoute) 我想看看 Global.asax 的路线,也想看看 AreaRegistration 的路线。
  • 添加了 MapRoute。我错过了什么吗?

标签: c# asp.net-mvc-3 asp.net-mvc-3-areas


【解决方案1】:

当您使用 URL /Home/Index 时,您不会经过该区域。在您的路由中定义了一个区域,带有 Administrator 前缀,并且该前缀在 url 中的存在允许路由引擎识别您在该区域内。

有一些possible workarounds有默认区域。

【讨论】:

  • 我刚刚阅读了您的推荐链接,并认为这可能对我的问题有用。谢谢!
  • 我有空闲时间来尝试这个解决方案。这是一项伟大的工作,但我认为它远非理想。我的问题是如果我尝试使用localhost:2813/Home/Index 访问,它应该重定向到 404 not found 而不是转到管理区域的 HomeController。
【解决方案2】:

如果您需要在区域内启动页面,请在默认区域的 HomeController (/Controllers/HomeController.cs) 中考虑这个:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return RedirectToAction("Index", "Home",
            new { area = "Administrator" });
    }
}

【讨论】:

    【解决方案3】:

    感谢您添加路线。如果您想要在 url 中没有指定区域时使用您的管理区域,您可以尝试将 Global.asax 中的路由替换为:

    routes.MapRoute( "Default", "{controller}/{action}/{id}", new { area = "Administrator", controller = "Home", action = "Index", id = UrlParameter.Optional }, new[] { "SampleWebsite.Areas.Administrator.Controllers" } );
    

    【讨论】:

    • 感谢您的回复。如果我访问http://localhost/Home/Index,这条路由会重定向到http://localhost/Administrator/Home/Index,对吗?但在此,我希望 http://localhost/Home/Index 重定向到错误 404:找不到页面。此控制器仅归管理员所有。项目的默认控制器没有 HomeController。
    • 不,不会有重定向。将调用来自管理区域的操作。如果您尝试处理访问权限,这不是好方法。区域不是为此而设计的。如果这是您想要的,请尝试查看授权过滤器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 2012-02-19
    • 2011-07-29
    • 2011-06-23
    • 1970-01-01
    • 2017-10-18
    相关资源
    最近更新 更多