Easy, simple HttpRuntime.Cache

A simple to use caching function that both gets and/or sets the key depending on results.

This is a post for my future self. – Hi future self! (wave).
The following is an easy to use caching function.

The function gets or updates based on results.
The wrapper helps create different cacheExpirations based on needs.
Usage:

Remove 'Try it out!' buttons from Swagger-ui

This is a quick way of removing the ‘try it out’ buttons from specific operation types, when using Swagger-ui for documentation

This will be a short one.
When using Swashbuckle to create documentation, you might want to remove the Try it out buttons, maybe all of them, or maybe just the ones on Put on Post operations
Well I’ve created a small script that is injected into swagger-ui through the SwaggerConfig.cs
The following script will remove the TryItOut buttons from all Put and Post operations.

The script is injected through the SwaggerConfig.cs like so

Remember that the .js file needs to be an Embedded Resource else it won’t work.

Global Pagination in WebAPI using ActionAttributeFilter

I like pagination, but only when it isn’t forced. In my perspective pagination should be opt-in not opt-out, so to speak.
Pagination should also, be easy to implement and not something we, as developers, should need to implement on a per entity basis.
The usual manual way to do this (also as described by the ASP.net docs) is adding pageSize and pageNumber as query-parameters on individual actions.

This is fine for smaller things, but not very DRY.
What I ended up doing was to create an ActionFilter.
With an ActionFilter you can “plug” 2 places,

  1. OnActionExecuted – which is before the request hits the method
  2. OnActionExecuting – which is after the request has hit the method.

ActionFilterExplanation
You can read about actionfilters here

Off to the code

So we need to create an ActionFilter, that will “plug” after the response has been attached to the HttpContext, where we look for specific query-parameters and then filter the response based on the query-parameters given.
Let’s list what we need to do.
1. Create a class that inherits from ActionFilterAttribute.
2. Look for specific query parameters.
3. Filter the response based on the query parameters.

1. Create a class that inherits from ActionFilterAttribute.

easy – we simply add a new class and inherit from ActionFilterAttribute and then override OnActionExecuted.

2. Look for specific query parameters.

Now, how do we get the query parameters? luckily the context object is full of good stuff!
context.Request.RequestUri.Query contains all of our queries and luckily there is a helper class to help us parse the query string.
using HttpUtility.ParseQueryString, we are able to .Get values based on the key (key-value pair)
So, to get, the value of “brand” from a request looking like the following http://myurl.com/cars?brand=hyundai

So for paging it would look something like this.

Note: I’m going the limit/offset route, instead of page/pagesize.
In the above we check for our specific query parameters (limit/offset) and set limit and offset accordingly

3. Filtering the response

Get the content from the response and Take & Skip the limit and offset (if needed).

4. Setting the newly filtered results as the “new” results of the request

Set the new result as the response content

5.

Now we just need to apply the filter to our entire project.
This happens in the WebApiConfig.cs file.

The full PagingFilter can be seen here


Now we never have to think about pagination support for specific actions, it’s already handled through our attribute.
Do note that this comes with a cost. – We are already loading the entire dataset into memory and then paginating it.
in production system, where performance is key – please use native DB paging support.