Web API 2 Attribute Routing

Posted by Community Admin on 04-Aug-2018 14:38

Web API 2 Attribute Routing

All Replies

Posted by Community Admin on 17-Jan-2017 00:00

I'm trying to use an Web Api Controller that has Route Attributes, but I'm unable to get it working.

Calling

GlobalConfiguration.Configuration.MapHttpAttributeRoutes();

when the site is initializing causes it to stay on the loading page forever. I don't see any errors being thrown, so it's hard to determine where it's going wrong.

Has anyone been able to get Route Attributes working?

Posted by Community Admin on 17-Jan-2017 00:00

In case anyone else runs into this problem, here's the solution I found:

I noticed that though there were no errors being logged, the loading screen was making a failed request to

/appstatus?count=2

The error message from the request was this:

<Error>
    <Message>An error has occurred.</Message>
     <ExceptionMessage>
        The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code.
    </ExceptionMessage>
    <ExceptionType>System.InvalidOperationException</ExceptionType>
    <StackTrace>
    at System.Web.Http.Routing.RouteCollectionRoute.get_SubRoutes() at System.Web.Http.Routing.RouteCollectionRoute.GetRouteData(String virtualPathRoot, HttpRequestMessage request) at System.Web.Http.WebHost.Routing.HttpWebRoute.GetRouteData(HttpContextBase httpContext)
    </StackTrace>
</Error>

 

After the code registering the Routes, I added the line 

GlobalConfiguration.Configuration.EnsureInitialized();

and the site loaded and the Route Attributes were working again.

This thread is closed