alex alex - 1 month ago 50
C# Question

WebApiConfig.Register() vs. GlobalConfiguration.Configure()

I have a project with a reference to Web API 2, and I'm working through some issues with routing. As far as I know, the correct way to approach this is to ensure that the following line of code is present in the

Global.asax
file:

GlobalConfiguration.Configure(WebApiConfig.Register);


But along the way, I picked up the following code:

WebApiConfig.Register(GlobalConfiguration.Configuration);


Global.asax
recognizes both lines of code as valid. What's the difference?

Answer

Source: Attribute Routing in ASP.NET Web API 2

Migrating From Web API 1

Prior to Web API 2, the Web API project templates generated code like this:

protected void Application_Start()
{
    // WARNING - Not compatible with attribute routing.
    WebApiConfig.Register(GlobalConfiguration.Configuration);
}

If attribute routing is enabled, this code will throw an exception. If you upgrade an existing Web API project to use attribute routing, make sure to update this configuration code to the following:

protected void Application_Start()
{
    // Pass a delegate to the Configure method.
    GlobalConfiguration.Configure(WebApiConfig.Register);
}
Comments