How to configure OData not handle non-odata exceptions in Web Api - asp.net-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.

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.

Related

Missing type map configuration or unsupported mapping. error in my aspnet boilerplate project

I have tried to make custom mapping between my DTO and Entity.
I have tried to use [AutoMapfrom(typeof)..] function to mapping.
I have tried to use ObjectMapper.Map function in my application layer for my service.
I'm Using Asp.Net boilerplate framework to create a wep api.
Even though I tried everything that I have written on the top, I still get Missing type map configuration or unsupported mapping. Error when I run my get method which is about my app. I will be glad if someone can help me.

What is the difference between AddMVC, AddMvcCore and AddRouting in ASP NETCore

I am looking for performance improvement in my ASP.net Core project
I am using graphql.net instead of REST in my project. So I don't need MVC mostly. But I also have a health check endpoint lets say api/health to check the service health. For that I need to add MVC to my application.
Initially I was using the addMVC extension, later I figured out that I should use addMVCCore instead of add addMVC to exclude unnecessary bundle.
Today I came across this article
https://hackernoon.com/iris-go-vs-net-core-kestrel-in-terms-of-http-performance-806195dc93d5
It talks about using AddRouting alone and using kestrel
I am already using kestrel.
I wanted to know what exactly differs among the three.
Can I use addRouting instead of addMVCCore if I just need a health check endpoint ? would that improve perf
AddMVC() internally calls the AddMVCCore() .
AddMVC() calls the required services for the MVC . like return view() from the action and it has only the minimum dependencies required to run the MVC framework
addMvcCore() calls only the mvc core services . it contains the return content("") and it doesn't contains the dataannotations ,auth filters , cors filter .

How to configure a named resource stream using OData in ASP.NET Web API

I am creating OData endpoints using ASP.NET Web API using v5.8.0 of this NuGet package. I have everything working and I can retrieve simple data such as strings and ints without an issue. However I would now like to extend one of the endpoints so that one of the items that can be retrieved is a binary image file. Having read up on OData I have decided to use named resource streams to achieve this.
The problem I have is that I can find very little information on how to implement this using the ODataController. The closest I can find is this blog post but it is using an older version of Web API and the code does not compile when I use it in my project.
So can somebody please supply a working example of how this should be implemented please.
Named streams (aka stream properties) are not currently supported in Web API OData (for OData v4). See the following issues:
Clarify the situation for streams
Document for Stream Support in OData V4

Getting the schema in swashbuck when using HttpResponseMessage, in WebApi

I'm attempting to use Swashbuckle to generate a swagger inteface for our API, we're returning HttpResponseMessage in our controller methods.
The docs indicate to use the [ResponseType(…)] attribute to indicate the actual return type. But that attribute is restricted to .net 4.5 (I believe, will gladly be proven wrong), and won't work in a .net4 project.
There are the XML comments, however I couldn't see how to use them the generate the schema information (I've taken a look through the unit tests in the project, but couldn't figure out if it's supported)

Queryable Web API 2 OData serialization

I have a lighweight setup where I am using Web API 2 with OData but do not create an "/odata" endpoint with its accompanying "/odata/$metadata" but instead just use standard ApiController's with attribute routing and Get methods marked with an override of [Queryable]: [InlineCountQueryable] detailed here Web API, OData, $inlinecount and testing.
It all works very well except that I can only expose dedicated model classes without any collections or object properties because the JSON serializer serializes the whole object graph. My ideal behavior would be to just have all collections ignored completely and object properties only included on $expand.
I believe this can be made to work because an OData Web Api 2 scaffold VS 2013 creates over an entity framework context has similar behavior, but I would like to keep this as lean as possible and avoid going that route as I found it to be a bit brittle and this service does not need to expose a $metadata description.
Update
I did some digging in the aspnet samples and found the "CustomODataFormatter" sample which seems to expose the machinery I would need to modify.. however the ODataSerializerProvider/ODataSerializer setup outlined there does not work with with attribute routing at all (any method defined with [Route("Some/Url")]).
Given that this seems to be what I need, does anyone know if attribute routing and the odata serializer provider mechanism should work together?
[JsonIgnore] or [DataMember] can be used to ignore properties for json.net serializer. webapi.odata 5.2 supports ODataRouteAttribute, an example is here.

Resources