asp.net mvc 是怎么算出来的?
我如何在控制器中创建一个方法,只需要输入一些参数,然后在单击表单提交后它就会计算出来? 在幕后,它如何找到正确的方法以及如何确定我只想要这些参数?
How is it that I can create a method in the controller and just put some arguments and it figures it out after I click on a form submit? Under the hood, how does it find the right method and how does it figure out that I just want those arguments?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简而言之:
路由引擎处理 HttpRequest,并检查请求的 URL。 当它找到第一个路由匹配时,它会创建一个新的
MvcRouteHandler
实例,并向其传递 RouteValueDictionary 中 URL 的分解标记。路由的
MvcRouteHandler
接受请求,并尝试实例化控制器类实例。 按照惯例,它会查找名为“XXXXXXController”的类,其中 X 被路由中的 {controller} 参数替换。一旦找到控制器,它就会调用路由的 {action} 参数给出的适当方法。 路由中存在的任何命名参数(例如 {id})都会作为参数传递给该方法。
基本上,ASP.Net MVC“知道”的一切都来自路由信息。 它无法凭空推测参数——它们必须来自路由解析。 如果请求的 URL 中不存在该信息,则无法将其传递到该方法中。
还应该注意的是,您可以通过使路由使用备用处理程序而不是
MvcRouteHandler
来覆盖框架的行为。 该框架具有很强的可扩展性,因此您可以在很多地方插入自定义功能。In a nutshell:
The routing engine handles the HttpRequest, and checks the requested URL. When it finds the first route match, it creates a new instance of
MvcRouteHandler
and passes it the broken-up tokens of the URL in a RouteValueDictionary.The route's
MvcRouteHandler
takes the request, and tries to instantiate a controller class instance. By convention, it looks for a class called "XXXXXXController", where the X's are replaced by the {controller} parameter in the route.Once it finds the controller, it invokes the appropriate method on it, given by the {action} parameter of the route. Any named arguments, such as {id}, that exist in the route, are passed as parameters to the method.
Basically, everything that ASP.Net MVC "knows" comes from the route information. It can't divine the parameters from thin air - they have to come from the route parsing. If the information isn't present in the requested URL, it can't be passed into the method.
It should also be noted that you can override the behaviour of the framework by making your routes use alternate handlers instead of
MvcRouteHandler
. The framework is quite extensible, so you can plug in custom functionality at many points.有相当多的代码用于控制器、操作和视图分辨率以及 ModelBinder。 如此之多,您可能最好研究框架的特定部分并提出更详细的问题以获得更多答案。
幸运的是,ASP.NET MVC 框架已经开源,因此,如果您对它是如何工作的感到好奇,您可以 获取代码 你自己看一下。 它的代码非常优秀,值得通读,您一定会学到一些东西。
然而,更重要的是,您应该查看 System.Web.Mvc.MvcHandler 和 System.Web.Mvc.ControllerActionInvoker 类,它们应该引导您走上回答问题的正确路径。
There's quite a bit of code in play for controller, action and view resolution, as well as the ModelBinders. So much that it'd probably be best for you to look into specific portions of the framework and ask a more detailed question to get much of an answer.
Luckily, the ASP.NET MVC framework has been open-sourced, so if you're curious as to how it all works, you can get the code and look through it yourself. Its excellent code to read through and you're sure to learn something.
More to the point of your question, however, you should look at the System.Web.Mvc.MvcHandler and System.Web.Mvc.ControllerActionInvoker classes, which should lead you down the right path for answering your questions.