Owin middleware, ASP.NET Web API and client’s IP address

Not so long ago, we discussed on this blog the possible ways of retrieving the client’s IP address in ASP.NET Web API.

With the latest changes in the Web API 2 infrastructure, and the emergence of Owin and the Katana project as the common glue between the underlying host and the web framework(s) running on it, it’s becoming natural to move these types of application-wide concerns (security, logging etc) to Owin middleware.

Let’s have a look at how you could - as an introductory example - obtain client’s IP address at the Owin middleware level, and why is it beneficial.

Scriptcs and multiline REPL!

A while ago, we added a killer REPL feature to the scriptcs project. Recently, we followed it up with adding support for one of the most popular requested features - multiline constructs for the REPL.

It’s not yet available in the stable version available via Chocolatey or on the master branch on Github, but you can get a taste of how it looks and feels by installing the nightly build or building from dev branch directly.

Let’s have a quick look together.

Building Spotify remote control service with ASP.NET Web API and SignalR

I have just published a new article over at my friends from Tech.pro (who have revamped their site this week too, and it looks awesome!). It’s my third one there, and once again we look at utilizing ASP.NET Web API, SignalR and bits of Knockout.JS. This time, however, it’s all single-user, as we will try to create a service that would allow you to remote control your Spotify client over HTTP.

IHttpActionResult – new way of creating responses in ASP.NET Web API 2

In the first version of Web API, you had two ways of creating response from an API action. Either return a specific object instance (or void) and let the Web API pipeline convert that to an HttpResponseMessage for you, or return a raw HttpResponseMessage. In that case, you had to construct it manually, and you bypassed all of the internal Web API mechanisms (formatters, content negotiation).

Now in Web API 2 (which was just released in beta) we get a third option, which is IHttpActionResult. It’s very simple, yet extremely powerful - so let’s explore it in this blog post.

Overriding filters in ASP.NET Web API vNext

One of the more annoying issues in the current version of ASP.NET Web API, is that it’s very difficult to override global/controller scoped filters at action level.

Perhaps you may have logging functionality or authorization filters, that you want to apply globally, and only exclude certain actions from participating in this process.

This problem is remedied in Web API vNext (version 5), and you can try that out with nightly builds.

Supporting only JSON in ASP.NET Web API – the right way

Sometimes (or perhaps, quite often?) you only need to support JSON in your ASP.NET Web API. In these cases, the ability of Web API pipeline to facilitate various media types is of no value for you - and you probably do not care at all about content negotiation.

In such situations it is often suggested to remove other media type formatters and only leave JsonMediaTypeFormatter. In fact, the author of this blog has earned almost 20 StackOverflow points for this type of suggestion.

However, there is even better way to solve this problem, and you’ll love it if you are into micro-optimizations.

Retrieving the client’s IP address in ASP.NET Web API

One of the common tasks, for logging or authorization purposes, is to obtain the IP address of the client accessing your Web API.

There are different ways to achieve this depending on whether you are web hosting (through HttpContext) or whether you are self hosting (through WCF’s RemoteEndpointMessageProperty).

To avoid the redundant code, we can unify those approaches under a single extension method, which you can use across all your projects.

scriptcs and using Azure Mobile Services from your scripts

Hopefully by now you have already heard about the sriptcs project, which allows you to write script based applications with C# and Nuget.

If not, have a look at the readme and the great introduction post by Scott Hanselman to get started.

Actually, just today, we have released v.0.4 of scriptcs! To celebrate that, let’s revisit one of our favorite topics - Azure Mobile Services - and how you can use it with scriptcs.

The future is now – OWIN and multi-hosting ASP.NET web applications

As you probably know, the ASP.NET team is publishing the latest ASP.NET Web API on a nightly MyGet feed, and you can grab them from there and play with the latest stuff without having to deal with all the hassle related to building from the source.

The same applies to Katana, a Microsoft generic Owin host, which also has its own MyGet nightly feed.

Let’s glimpse into the near future and have a look at putting the latest Katana bits to play with ASP.NET Web API and other frameworks.

ASP.NET Web API and greedy query string parameter binding

Following the last article on parameter binding from URI, I received an interesting question - “what if I wanted to prevent binding parameters from query string and only allow binding from route values”? In other words, prevent passing values to actions via query strings and only look for them in the route itself (to avoid duplicate ways of reaching the same endpoint).

This is possible in Web API - let’s explore how you’d go about implementing it.

More after the jump.

About


Hi! I'm Filip W., a software architect from Zürich 🇨🇭. I like Toronto Maple Leafs 🇨🇦, Rancid and quantum computing. Oh, and I love the Lowlands 🏴󠁧󠁢󠁳󠁣󠁴󠁿.

You can find me on Github, on Mastodon and on Bluesky.

My Introduction to Quantum Computing with Q# and QDK book
Microsoft MVP