【问题标题】:C# MVC 3 Root Route doesn't seem to workC# MVC 3 根路由似乎不起作用
【发布时间】:2011-02-14 03:13:56
【问题描述】:

编辑:对不起,我解释得很糟糕。基本上,在下面的示例中,我希望“this-is-handled-by-content-controller”成为“id”,因此我可以在 ContentController 中将其作为操作参数获取,但我想通过根访问它网站的名称,例如 mysite.com/this-is-not-passed-to-homecontroller。

我正在尝试创建一个将转到单独的控制器(而不是主控制器)的根路由。

我已经按照在其他地方发布的实现 IRouteConstraint 的“RootController”示例进行了操作,但它似乎不起作用,我已经在这上面浪费了几个小时!

基本上,我有一个 LoginController、一个 HomeController 和一个 ContentController。

我希望能够通过转到http://mysite/ 来查看 HomeController/Index。我希望能够通过转到http://mysite/Login 来查看 LoginController/Index。但是.. 如果发生任何其他结果,我希望调用 ContentController/Index,例如:http:/mysite/this-is-handled-by-content-controller

有没有一种优雅的方法可以做到这一点?

这是我最后一次尝试。我已经剪切/粘贴/复制/抓挠我的头很多次了,有点乱:

routes.MapRoute(
            "ContentPages",
            "{action}",
            new { Area = "", controller = "ContentPages", action = "View", id = UrlParameter.Optional },
            new RootActionConstraint()
            );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { Area = "", controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
            new string[] { "Website.Controllers" }
        );

非常感谢任何帮助!

化学

【问题讨论】:

标签: c# asp.net-mvc model-view-controller asp.net-mvc-3


【解决方案1】:

我会做类似的事情,但如果您将来继续添加更多控制器,这可能不是最佳解决方案。

routes.MapRoute(
    "HomePage",
    "",
    new { controller = "Home", action = "Index", id="" }
);
routes.MapRoute(
    "Home",
    "home/{action}/{id}",
    new { controller = "Home", action = "Index", id="" }
);
routes.MapRoute(
    "Login",
    "Login/{action}/{id}",
    new { controller = "Login", action = "Index", id="" }
);
//... if you have other controller, specify the name here

routes.MapRoute(
    "Content",
    "{*id}",
    new { controller = "Content", action = "Index", id="" }
);

第一个路由是你的 youwebsite.com/,它调用你的 Home->Index。第二条路线是针对您的 Home Controller (yourwebsite.com/home/ACTION) 上的其他操作。

第三个是你的 LoginController (yourwebsite.com/login/ACTION)。

最后一个用于您的 Content->Index (yourwebsite.com/anything-that-goes-here)。

public ActionResult Index(string id)
{
  // id is "anything-that-goes-here
}

【讨论】:

  • "最后一个是你的内容->索引 (yourwebsite.com/anything-that-goes-here)"。那条路线行不通。
  • 被截断了。当我声明默认路由 ({controller}/{action}/{id}) 时,该路由不起作用。
【解决方案2】:

假设您有 ContentController.Index(string id) 来处理匹配约束的路由,这应该可以工作:

routes.MapRoute(
        "ContentPages",
        "{id}",
        new { Area = "", controller = "Content", action = "Index" },
        new { id = new RootActionConstraint() }
        );

routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}",
        new { Area = "", controller = "Home", action = "Index", id = UrlParameter.Optional },
        new string[] { "Website.Controllers" }
    );

【讨论】:

  • 什么是 RootActionConstraint?我出于好奇搜索了它,并且冒着出现无法搜索的风险,我什么也没找到。该约束可以是自定义构建的吗?
  • 是的,OP 提到RootActionConstraintIRouteConstraint 的自定义实现。
猜你喜欢
  • 2012-12-12
  • 2021-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-26
相关资源
最近更新 更多