C#MVC 3根路由不';好像不行
编辑:对不起,我解释得不好。基本上,在下面的示例中,我希望“this is handled by content controller”是“id”,因此我可以在ContentController中获取它作为操作参数,但我希望通过站点的根访问它,例如mysite.com/this-is-not-passed-to-homecontroller 我正在尝试创建一个根路由,该路由将转到一个单独的控制器(而不是主控制器) 我遵循了其他地方发布的实现IRouteConstraint的“RootController”示例,但它似乎不起作用,我已经为此浪费了几个小时 基本上,我有一个LoginController、一个HomeController和一个ContentController 我希望能够通过转到查看HomeController/索引。我想通过转到可以查看LoginController/索引。但是我希望在出现任何其他结果时调用ContentController/索引,例如:http:/mysite/这由ContentController处理 有没有一种优雅的方法可以做到这一点 这是我最后一次尝试。。我已经剪/粘贴/复制/搔了很多次头,有点乱:C#MVC 3根路由不';好像不行,c#,asp.net-mvc,model-view-controller,asp.net-mvc-3,C#,Asp.net Mvc,Model View Controller,Asp.net Mvc 3,编辑:对不起,我解释得不好。基本上,在下面的示例中,我希望“this is handled by content controller”是“id”,因此我可以在ContentController中获取它作为操作参数,但我希望通过站点的根访问它,例如mysite.com/this-is-not-passed-to-homecontroller 我正在尝试创建一个根路由,该路由将转到一个单独的控制器(而不是主控制器) 我遵循了其他地方发布的实现IRouteConstraint的“RootContro
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" }
);
非常感谢您的帮助
chem假设您有
ContentController.Index(字符串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" }
);
假设您有
ContentController.Index(字符串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" }
);
我会做一些类似的事情,不过如果您以后继续添加更多控制器,这可能不是最好的解决方案
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/,它叫你的家->索引。第二条路线是在家庭控制器(yourwebsite.com/Home/ACTION)上执行其他操作
第三个是你的登录控制器(yourwebsite.com/login/ACTION)
最后一个是你的内容->索引(yourwebsite.com/这里的任何内容)
我会做一些类似的事情,不过如果您以后继续添加更多控制器,这可能不是最好的解决方案
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/,它叫你的家->索引。第二条路线是在家庭控制器(yourwebsite.com/Home/ACTION)上执行其他操作
第三个是你的登录控制器(yourwebsite.com/login/ACTION)
最后一个是你的内容->索引(yourwebsite.com/这里的任何内容)
这可能涵盖了您正在寻找的内容。处理不匹配的路线并不是路线引擎的工作。这可能涵盖了您正在寻找的内容。处理不匹配的路线并不是路线引擎的工作。什么是RootActionConstraint?我好奇地搜索它,冒着看起来无法搜索的风险,我什么也没找到。该约束可能是自定义生成的吗?是的,
RootActionConstraint
被OP提到是iroutConstraint
的自定义实现。什么是RootActionConstraint?我好奇地搜索它,冒着看起来无法搜索的风险,我什么也没找到。该约束可能是自定义生成的吗?是的,RootActionConstraint
被OP提到是iroutConstraint
“的自定义实现,最后一个用于内容->索引(yourwebsite.com/此处的任何内容)”。那条路不通,被切断了。当我声明默认路由({controller}/{action}/{id})时,该路由不起作用。“最后一个路由用于内容->索引(yourwebsite.com/此处的任何内容)”。那条路不通,被切断了。当我声明了默认路由({controller}/{action}/{id})时,该路由不起作用。