首页 > 解决方案 > 从头开始制作控制器类

问题描述

所以我制作了这个 Web 服务器,这里有一些用于设计的 UML。

HTTP Web 服务器的 UML

我将 URI 的部分保存到 astring[]以便 URI"/school/students"将被解码为{"school","students"}. 然后将URI映射到资源和子资源的集合上;它链接到适当的方法路由(它定义了在输入特定 URI 和 HTTP 方法时发回的 HTTP 响应)。

这是设置一些基本路由的示例:

Resource pupils = new Resource("pupils");

        HTTPResponse pupilsResponse = new HTTPResponse(
                    HTTPResponse.StatusCodes.OK,
                    HTTPMessage.MIMETypes.PLAIN_TEXT,
                    "pupils"
                    );

        pupils.AddMethodRoute(new MethodRoute(
                HTTPRequest.RequestMethodType.GET,
                pupilsResponse
                )
            );

        Resource year9 = new Resource("year9");

        HTTPResponse year9Response = new HTTPResponse(
                    HTTPResponse.StatusCodes.OK,
                    HTTPMessage.MIMETypes.PLAIN_TEXT,
                    "year 9"
                    );

        year9.AddMethodRoute(new MethodRoute(
                HTTPRequest.RequestMethodType.GET,
                year9Response
                )
            );

        Resource year10 = new Resource("year10");

        HTTPResponse year10Response = new HTTPResponse(
                    HTTPResponse.StatusCodes.OK,
                    HTTPMessage.MIMETypes.PLAIN_TEXT,
                    "year 10"
                    );

        year10.AddMethodRoute(new MethodRoute(
                HTTPRequest.RequestMethodType.GET,
                year10Response
                )
            );

        pupils.AddSubResource(year10);
        pupils.AddSubResource(year9);

        Resource school = new Resource("school");

        HTTPResponse schoolResponse = new HTTPResponse(
                    HTTPResponse.StatusCodes.OK,
                    HTTPMessage.MIMETypes.PLAIN_TEXT,
                    "school"
                    );

        school.AddMethodRoute(new MethodRoute(
                HTTPRequest.RequestMethodType.GET,
                schoolResponse
                )
            );

        HTTPServer Server = new HTTPServer(3000, school);

        Server.AddSubRoute(pupils);

        Server.Listen();

这似乎有点笨拙的方式来设置路由。我将如何添加某种控制器?我正在考虑使用回调方法,但我想要某种控制器类,例如 ASP.NET MVC ......关于如何使这个更整洁和更清洁的任何想法?仅仅为我指出正确的方向就太棒了!

标签: c#.nethttpserveruml

解决方案


推荐阅读