[英]ASP.net Web API 2:How WebApiConfig.Register is being called without passing parameter in Global.asax.cs file
I am creating restful service using ASP.Net WEB API 2.0. 我正在使用ASP.Net WEB API 2.0创建Restful服务。 While reviewing the code, below is the code of RouteConfig.cs class file:
在查看代码时,以下是RouteConfig.cs类文件的代码:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
In Global.asax.cs file the RegisterRoutes method of RouteConfig class is being called as below: 在Global.asax.cs文件中,RouteConfig类的RegisterRoutes方法被调用,如下所示:
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
I am very curious to know : 我很好奇:
GlobalConfiguration.Configure
accepts Action<HttpConfiguration>
as a parameter. GlobalConfiguration.Configure
接受Action<HttpConfiguration>
作为参数。 This is in a way "reference to a fucntion". 这是“引用功能”的一种方式。 When you use
WebApiConfig.Register
without ()
it's not the call to a function but rather a delegate that is passed as a parameter. 使用不带
()
的WebApiConfig.Register
,不是对函数的调用,而是作为参数传递的委托。 WebApiConfig.Register
has a correct signature, ie accepts HttpConfiguration
as a parameter, that's why you can just pass it on to GlobalConfiguration.Configure
. WebApiConfig.Register
具有正确的签名,即接受HttpConfiguration
作为参数,这就是为什么您可以将其传递给GlobalConfiguration.Configure
。
Later the framework will call the method that you have passed and supply the parameter. 稍后,框架将调用您传递的方法并提供参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.