【问题标题】:Parameter only route always being triggered MVC3仅参数路由总是被触发 MVC3
【发布时间】:2012-06-16 05:04:25
【问题描述】:

我有两条非常简单的路线

       routes.MapRoute(
            "post", // Route name
            postPage + "/{slug}", // URL with parameters
            new { controller = "Home", action = "Article" } // Parameter defaults
        );

        routes.MapRoute(
            "page", // Route name
            "{slug}", // URL with parameters
            new { controller = "Home", action = "Page", slug = homePage} // Parameter defaults
        );

这是我的控制器逻辑

    public ActionResult Article(string slug)
    {
        return View(repo.GetPost(slug));
    }

    public ActionResult Page(string slug)
    {
        if (slug.ToLower() == MetaData.PostsPage.ToLower())
            return View("listPosts", repo.GetAllPosts());
        else
            return View("page", repo.GetPage(slug));
    }

homePage 和 postPage 是从数据库中的值设置的。允许用户定义默认页面以及显示帖子的页面。

我的问题是在添加名为“Admin”的区域时出现的。我在 RouteTable 中添加了一个控制器

    context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );

现在,当用户访问 Admin/Account/Logon 时,页面加载正常,但我的调试器仍然尝试进入 Home 控制器和 Page 操作。但是 RouteDebugger 说它与当前请求不匹配。我很困惑如何解决这个问题。

RouteDebugger 截图:http://i.stack.imgur.com/7cpHm.png 调试器进入我的 HomeControler 页面 AtionResult:http://i.stack.imgur.com/uSJBK.png

【问题讨论】:

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


    【解决方案1】:

    实际上问题是,Area 路由覆盖了全局路由,为了区分这两种路由,在 adminAreaRegistraton.cs 文件的 context.MapRoute 方法中设置 area 控制器的相关命名空间。即

    context.MapRoute(
     "admin_default",
     "admin/{controller}/{action}/{id}",
     new { controller = "Home", action = "Index", id = UrlParameter.Optional },
     null,
     new string[] { "MVCApplication1.Areas.admin.Controllers" }
    );
    

    【讨论】:

    • 我将它添加到我的 admin_default 中,但它仍在尝试在我的主页控制器的页面操作中运行代码
    【解决方案2】:

    我发现了问题。

    我在网站的主要区域设置了 favicon.ico,但在管理区域没有设置。 因此,当我进入管理区域时,浏览器向 favicon.ico 发出了请求,该请求被该路由获取。这就是为什么我的路线在 RouteDebugger 中看起来不错的原因,因为它们确实如此。

    感谢昆丹的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-29
      • 2018-07-07
      • 2013-09-12
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 2012-03-29
      • 1970-01-01
      相关资源
      最近更新 更多