Use a Custom Content Negotiatior to always return JSON in ASP.NET Web API

The ASP.NET Web API pipeline decides the media formatter to use depending on the request. This complex decision making process occurs during the content negotiation and is handled by the DefaultContentNegotiator. If we need to support only a single format, say JSON, we can replace the default negotiator with …

Read

Pass PL/SQL associative array to Oracle stored procedure from C#

The ArrayBind feature of ODP.NET comes allows passing arrays to a stored procedure. Since all parameters should be arrays of equal length, it may not be the right choice if not all parameters are arrays. Say, we want to call a procedure that takes a user name and an array of role names …

Read

Sort List with string property names

For .NET 3.5 and later we can use the System.Linq.Dynamic library available for download from NuGet If we have a collection of Car objects with properties Make, Model, Year and Price... class Car { public string Make { get; set; } public string Model { get; set; } public int Year { get; …

Read

Inject IPrincipal in ASP.NET Web API Controller

In ASP.NET MVC the current IPrincipal can be automatically made available to action methods by implementing the System.Web.Mvc.IModelBinder interface and returning the current authenticated user from HttpContext.User. We can accomplish the same in Web API using the HttpParameterBinding class. namespace App.Web.Http.Helpers { public …

Read

OracleDataReader GetOrDefault() extension method

The various GetXXX methods of the OracleDataReader class take a zero-based column index instead of the column name. Referring columns by index instead of name leads to hard to understand code. Additionally, the code may be susceptible to errors if the column order is modified. …

Read