Bhala
Bhala

Reputation: 41

How to configure OData not handle non-odata exceptions in Web Api

I want to use OData in Web Api for few Action methods in a Controller. What is happening is that once i enable OData in Web Api, the error message format is getting changes for all error . Is there any way to configure Odata only for specific controller/action routes.

Error Message before Enabling OData looks like:

{"Message":"User Name/Password are invalid ."}

Error Message after Enabling OData looks like:

{
  "odata.error":{
    "message":{
      "lang":"en-US","value":"User Name/Password are invalid ."
    }
  }
}

I would like to configure OData to handle only specific controllers so that rest of the APIs have no impact of OData setting. Your help is appreciated.

Upvotes: 4

Views: 2458

Answers (1)

Youssef Moussaoui
Youssef Moussaoui

Reputation: 12395

One of the big changes we made between RC and RTM is that we've completely removed the EnableOData extension method. We realized that registering OData formatters globally was a bad idea because it impacts controllers regardless of whether they're meant to be OData controllers.

So, in our v1 release for OData and in our current nightly builds, we've added a new base class called ODataController. If you derive from ODataController (or EntitySetController), you will automatically get support for OData just for that controller. It shouldn't affect the rest of your controllers the way it does now. You should also use config.Routes.MapODataRoute instead of EnableOData.

You can install our latest nightly build using these instructions:

http://blogs.msdn.com/b/henrikn/archive/2012/06/01/using-nightly-asp-net-web-stack-nuget-packages-with-vs-2012-rc.aspx

It should be pretty stable at this point.

Upvotes: 3

Related Questions