RouteTable.Routes RouteCollection?

发布于 2024-07-15 22:41:33 字数 1339 浏览 6 评论 0原文

我随处看到的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赤濁 2024-07-22 22:41:33

单元测试。

通过从 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文