c# - 从头开始制作控制器类
问题描述
所以我制作了这个 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 ......关于如何使这个更整洁和更清洁的任何想法?仅仅为我指出正确的方向就太棒了!
解决方案
推荐阅读
- php - 仅打印 foreach 中每个重复项的最后一项
- html - 如何为具有相同父级的两个嵌套元素设置样式
- r - 堆叠直方图上的颜色标签(在 R 中)
- typescript - 带有枚举的 TypeScript 条件类型
- file - TYPO3 链接到本地文件,例如“file:///C:/SomeFile”
- java - MapperRegistry 不知道类型接口 com.example.JSF.mappings.StudentMapper
- javascript - 迭代 JS 对象并执行每个函数
- html - 如何计算 jquery 中每个无序列表的列表项数
- xml - SAML SP:信任 IdP 的证书
- javascript - 使用异步迭代器在 Node 中并行批处理请求和处理