简体   繁体   English

ASP.net Web API 2:如何在不传递Global.asax.cs文件中的参数的情况下调用WebApiConfig.Register

[英]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 : 我很好奇:

  1. How WebApiConfig.Register method is being called as we are not passing any parameter explicitly. WebApiConfig.Register方法的调用方式,因为我们没有显式传递任何参数。

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.

相关问题 ASP.NET MVC 4中的Global.asax.cs文件中正在执行什么模式? - What pattern is being done in the Global.asax.cs file in ASP.NET MVC 4? ASP.NET MVC:如何在Global.asax.cs中的Application_Start()中检测浏览器宽度 - ASP.NET MVC: How to Detect Browser Width in Application_Start() in Global.asax.cs ASP.net MVC 5 - 如何从 Global.asax.cs 中的 session_end() 事件重定向到操作方法 - ASP.net MVC 5 - How to redirect to action method from from session_end() event in Global.asax.cs 在Global.asax.cs中设置cookie,并在asp.net mvc c#视图中获取它 - Set cookie in Global.asax.cs and get it in the view of asp.net mvc c# ASP.NET MVC 应用程序在 Global.asax.cs 中的 VS 2019 解析器错误 - ASP.NET MVC application in VS 2019 parser error in Global.asax.cs 在Asp .NET MVC 2中的Global.asax.cs中识别MVC请求 - Identify MVC request in Global.asax.cs in Asp .NET MVC 2 如何在Global.asax.cs文件中全局声明Filter属性 - How to declare Filter attribute globally in Global.asax.cs file WebApiConfig.Register未运行 - WebApiConfig.Register is not run 没有global.asax的ASP.NET网页路由如何工作 - How does routing for ASP.NET Web Pages work without global.asax 在Global.asax.cs中注册类 - Registering classes in Global.asax.cs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM