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?
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With