Global exception handling in OWIN middleware

Ok, so this was easier than anticipated, thanks for @Khalid for the heads up, I have ended up creating an owin middleware named OwinExceptionHandlerMiddleware which is dedicated for handling any exception happening in any Owin Middleware (logging it and manipulating the response before returning it to the client). You need to register this middleware as … Read more

Implement HTTP Cache (ETag) in ASP.NET Core Web API

After a while trying to make it work with middleware I figured out that MVC action filters are actually better suited for this functionality. public class ETagFilter : Attribute, IActionFilter { private readonly int[] _statusCodes; public ETagFilter(params int[] statusCodes) { _statusCodes = statusCodes; if (statusCodes.Length == 0) _statusCodes = new[] { 200 }; } public … Read more

Exceptions in ASP.NET Web API custom exception handler never reach top level when CORS is enabled

I have found the source of confusion. It seems, WebAPI by default is using this exception handler: https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Http/ExceptionHandling/DefaultExceptionHandler.cs and it has major differences from the suggested exception handling in this article: http://www.asp.net/web-api/overview/web-api-routing-and-actions/web-api-global-error-handling see chapter “Appendix: Base Class Details”, where the code of default exception base class is given. In the example it checks for IsOutermostCatchBlock … Read more

Overload web api action method based on parameter type

This kind of scenario is not well supported by the standard routing methods. You may want to use attribute based routing instead as this gives you a lot more flexibility. Specifically look at the route constraints where you can route by the type: // Type constraints [GET(“Int/{x:int}”)] [GET(“Guid/{x:guid}”)] Anything else will turn into a bit … Read more

How to access all querystring parameters as a dictionary

You can use the GetQueryNameValuePairs extension method on the HttpRequestMessage to get the parsed query string as a collection of key-value pairs. public IHttpActionResult Get() { var queryString = this.Request.GetQueryNameValuePairs(); } And you can create some further extension methods to make it eaiser to work with as described here: WebAPI: Getting Headers, QueryString and Cookie … Read more

Trying to get the user-agent from request in asp.net web api self host

The absolutely simplest way to get the full user-agent from inside a WebAPI-controller is by doing this: var userAgent = Request.Headers.UserAgent.ToString(); It gives exactly the same result as doing the manual step like this: // var headers = request.Headers.GetValues(“User-Agent”); // var userAgent = string.Join(” “, headers);

Web API: how to access multipart form values when using MultipartMemoryStreamProvider?

Updated 4/28/2015 You could create a custom provider based on MultipartFormDataRemoteStreamProvider. Example: public class CustomMultipartFormDataProvider : MultipartFormDataRemoteStreamProvider { public override RemoteStreamInfo GetRemoteStream(HttpContent parent, HttpContentHeaders headers) { return new RemoteStreamInfo( remoteStream: new MemoryStream(), location: string.Empty, fileName: string.Empty); } } Updated Custom In-memory MultiaprtFormDataStreamProvider: public class InMemoryMultipartFormDataStreamProvider : MultipartStreamProvider { private NameValueCollection _formData = new NameValueCollection(); private … Read more

IHttpActionResult and helper methods in ASP.NET Core

IHttpActionResult is now effectively IActionResult, and to return an Ok with a return object, you’d use return new ObjectResult(…); So effectively something like this: public IActionResult Get(int id) { if (id == 1) return HttpNotFound(“not found!”); return new ObjectResult(“value: ” + id); } Here’s a good article with more detail: http://www.asp.net/vnext/overview/aspnet-vnext/create-a-web-api-with-mvc-6

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)