Running ASP.NET Core content negotiation by hand
When you are building flexible HTTP APIs, supporting a wide array of different clients, it is common to rely on the process of content negotiation, to allow each client to interact with the API in the most convenient way - be it JSON, XML, Protobuf, Messagepack or any other media type on which both the client and the server can agree.
I have blogged about content negotiation (or in short: conneg) a few times in the past (for example here or here, in ASP.NET Core context). Today I’d like to show you how - in ASP.NET Core - to easily run conneg by hand, instead of relying on the built-in MVC conneg mechanisms.
dotnet-script 0.26 is out – with a boatload of new features
After a short holiday break, the dotnet-script release train is back at full speed! This week we released 0.26 which can be installed from NuGet - as the .NET CLI global tool - or from Chocolatey.
Let me do a quick roundup of what’s new.
Azure Blob Storage IFileProvider for ASP.NET Core
As part of my recent talks on ASP.NET core, I have been showing how to build a custom IFileProvider for ASP.NET Core. The example that I was using was Azure Blob Storage - and exposing files from there as if they were local files that are part of your application.
I have pushed that code to Github and decided to package it as Nuget package, which, hopefully, someone will find useful.
Centralized exception handling and request validation in ASP.NET Core
One of the most common things that I have seen developers working with ASP.NET Core struggle with, is the way to centrally and consistently handle application errors and input validation. Those seemingly different topics are really two sides of the same coin.
More often than not, exceptions are just allowed to bubble all the way up and left unhandled, leaving the framework the responsibility to convert them to a generic 500 errors. In many other situations, exception handling is fragmented and happens in certain individual controllers only. With regard to input validation, we often have completely customized ways of notifying the client about input issues or - at best - we leave everything to the framework and let it work its defaults via the ModelState functionality.
What I wanted to show you today is how you can introduce a consistent, centralized way of handling exceptions and request validation in an ASP.NET Core web application.
Solution-wide Nuget package version handling with MsBuild 15+
Last year I blogged about a way to handle NuGet package versions at the solution level for .NET SDK-based csproj project files (so those using
That approach worked reasonably well, but was entirely custom - as it simply relied on defining reusable MsBuild properties to handled the versions, which created a bit of overhead.
With MsBuild 15 and newer, you can actually do it in a much more elegant way. Let’s have a look.
Overriding externally set headers and HTTP status codes in ASP.NET Core
I was working on an interesting issue in an ASP.NET Core recently. An external framework was responsible for creating an HTTP Response, and I was only in control of a little component that customized some internal behaviours (via a relevant extensibility point), without being able to influence the final response sent over HTTP.
This is common if you think about extending things like CMS systems or specialized services like for example Identity Server. In those situations, more often than not, the framework would be highly opinionated in what it is trying to do at the HTTP boundaries and as a result, trying to override the HTTP status codes or headers it produces may not be easy.
Let’s have a look at a simple generic workaround.
Controllers as action filters in ASP.NET Core MVC
It is common to leverage action filters when building MVC applications - this was the case in classic ASP.NET MVC, in ASP.NET Web API and is a still widely used technique (with much richer support!) in ASP.NET Core MVC.
What is not commonly known though, is that it’s possible for controllers to act as their own filters - so let’s have a look at this feature today.
Dependency injection into actions in ASP.NET Core MVC 2.1
A few years ago I blogged about dependency injection directly into actions in ASP.NET Web API. The idea is quite simple - instead of injecting all of your dependencies via the controller’s constructor, you can inject them as parameters into the action handling a given HTTP request.
This can help you prevent over-injection in the constructor; I also like this approach because it’s very verbose and clear - an action explicitly manifests what it needs to handle the request and gets just that, nothing more.
It actually works out of the box in ASP.NET Core, so let’s have a look at that and some related configuration.
dotnet-script – now on .NET Core 2.1 runtime, with C# 7.3 support and more!
In the previous post, I announced that dotnet-script is now built as global tool and can be installed using .NET Core 2.1 preview SDK. However, by that time it was still based on .NET Core 2.0 runtime (it was possible to build and install .NET Core 2.0 tools using .NET Core 2.1 preview SDK).
Now we have even more cool news to announce - because dotnet-script is now a true .NET Core 2.1 application, and can be used to run scripts on top of .NET Core 2.1 runtime. We release quite often so let me do a quick rundown of all the new features since my last blog post.
dotnet-script now available as .NET Core SDK 2.1 global tool
Yesterday we released version 0.20.0 of dotnet-script - a command line tool that allows you to run C# scripts (.csx) in the .NET Core runtime.
The highlight of this release is that it is now avaialble as .NET Core SDK 2.1 global tool, which provides an excellent acquisition/installation story directly from nuget.
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 🏴.
Recent Posts
- 2024/12/20, Running Phi Inference in .NET Applications with Strathweb Phi Engine
- 2024/12/16, Decorating a Quantum Christmas Tree with Q# and Qiskit
- 2024/12/12, Generating OpenQASM from Q# code
- 2024/11/22, Simplifying the AI workflow: Access different types of model deployments with Azure AI Inference
- 2024/11/15, Strathweb Phi Engine - now with Safe Tensors support
Categories
- ai (17)
- ai search (5)
- apache cordova (1)
- asp.net 5 (17)
- asp.net core (47)
- asp.net mvc (35)
- asp.net mvc 6 (7)
- asp.net vnext (6)
- asp.net web api (96)
- astronomy (1)
- autogen (1)
- azure (22)
- azure service bus (1)
- azure-devops (1)
- benchmark dotnet (1)
- bing maps (1)
- blazor (2)
- c plus (2)
- c-sharp (155)
- cryptography (5)
- csharp (6)
- csharp 10 (2)
- dnx (3)
- dotnet-cli (2)
- dotnet-script (11)
- duende (4)
- editorconfig (1)
- entity framework (2)
- espn api (2)
- events (1)
- ffi (4)
- fsharp (1)
- git (1)
- glimpse (1)
- html5 (4)
- identity server (2)
- iis (2)
- il (1)
- intro to qc (19)
- ios (5)
- javascript (9)
- jquery (4)
- jquery mobile metro (1)
- katana (2)
- kindle (1)
- knockout.js (8)
- kotlin (2)
- last.fm api (2)
- linq (1)
- mac (2)
- macos (1)
- mathematica (1)
- msbuild (3)
- mvc core (3)
- nancy (2)
- native (1)
- net (144)
- net 5 (3)
- net 6 (5)
- net 7 (7)
- net 8 (3)
- net 9 (1)
- net core (49)
- net sdk (2)
- ninject (2)
- odata (4)
- oidc (2)
- omnisharp (13)
- openai (11)
- osx (2)
- owin (5)
- phi (5)
- php (1)
- python (1)
- q-sharp (36)
- qir (3)
- qiskit (1)
- quantum computing (40)
- roslyn (30)
- rust (5)
- scriptcs (11)
- scripting (9)
- security (8)
- servicestack (2)
- signalr (8)
- swift (8)
- testing (5)
- twitter boostrap (1)
- typescript (1)
- visual studio (4)
- visual studio code (11)
- wasi (3)
- wasm (3)
- windows phone 7 (1)
- wordpress (1)
- wpf (2)