RouteTable.Routes RouteCollection?
我随处看到的 MVC 风格路由的示例是这样的:
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.Add(new Route
(
"Category/{action}/{categoryName}"
, new CategoryRouteHandler()
));
}
What's the Reason to pass the RouteTable.Routes collection to RegisterRoutes()? 为什么不只是:
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
public static void RegisterRoutes()
{
RouteTable.Routes.Add(new Route
(
"Category/{action}/{categoryName}"
, new CategoryRouteHandler()
));
}
除了 RouteTable.Routes 之外,还会将路由添加到哪些 RouteCollection 中? RouteTable.Routes 不是 Web 应用程序的 RouteCollection 吗?
我有一个具有 Map() 方法的特定 IRouteHandler:
public class ChatRouteHandler : IRouteHandler
{
private static bool mapped;
public void Map()
{
if (!ChatRouteHandler.mapped)
{
RouteTable.Routes.Add
(
new Route("chat/{room}/{date}",
new ChatRouteHandler())
);
}
}
Map() 是否应该接受 RouteCollection 而不是添加到 RouteTable.Routes 集合中? 同样,这个 IRouteHandler 会被添加到其他什么 RouteCollection 中?
The example I see everywhere for MVC-style routing is something like this:
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.Add(new Route
(
"Category/{action}/{categoryName}"
, new CategoryRouteHandler()
));
}
What's the reason to pass the RouteTable.Routes collection to RegisterRoutes()? Why not just:
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
public static void RegisterRoutes()
{
RouteTable.Routes.Add(new Route
(
"Category/{action}/{categoryName}"
, new CategoryRouteHandler()
));
}
What RouteCollection besides RouteTable.Routes would a route be added to? Isn't RouteTable.Routes the RouteCollection for the web application?
I have a particular IRouteHandler with has a Map() method:
public class ChatRouteHandler : IRouteHandler
{
private static bool mapped;
public void Map()
{
if (!ChatRouteHandler.mapped)
{
RouteTable.Routes.Add
(
new Route("chat/{room}/{date}",
new ChatRouteHandler())
);
}
}
Is there a reason that Map() should accept a RouteCollection and not add to the RouteTable.Routes collection? Again, what other RouteCollection would this IRouteHandler be added to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
单元测试。
通过从 RouteTable.Routes 解耦(通过参数传递)注册,您可以选择在单元测试中使用哪个 RouteCollection 进行注册。
恕我直言,到处写 paths.Add() 而不是 RouteTable.Routes.Add() 也更好。
“Map() 是否应该接受 RouteCollection 而不是添加到 RouteTable.Routes 集合中?”
我将这个问题解释为“为什么 RouteCollection 没有 MapRoute 方法,而是扩展方法?”。
是的,RouteCollection 在 System.WEb.Routing 中,而 MapRoute 扩展方法在 System.Web.Mvc 中。 MapRoute() 依赖于 MvcHandler,而 System.Web.Routing 对此一无所知。
Unit testing.
By decoupling (through argument passing) registration from RouteTable.Routes you can choose which RouteCollection is used for registration in unit testing.
IMHO, it's also nicer to write routes.Add() instead of RouteTable.Routes.Add() everywhere.
"Is there a reason that Map() should accept a RouteCollection and not add to the RouteTable.Routes collection?"
I'm interpreting this question as "Why doesn't RouteCollection have the MapRoute method, instead of an extension method?".
Yeah, RouteCollection is in System.WEb.Routing, whilst MapRoute extension method is in System.Web.Mvc. MapRoute() depends on MvcHandler, which System.Web.Routing has no idea about.