Enabling [ApiController] globally in ASP.NET Core 2.2
A while ago we looked at how ApiControllerAttribute can be used in ASP.NET Core MVC to make Web API authoring a little bit more developer friendly.
Today let’s have a look at a tiny little improvement in ASP.NET Core 2.2 - the ability to set it up globally (and perhaps a few other things).
Collectible assemblies in .NET Core 3.0
Since the beginning of .NET Core, the one feature that I have been most anxiously waiting for, has been support for collectible assemblies. It took a while (a while!), but finally, in .NET Core 3.0 (at the time of writing 3.0.0-preview-27122-01 from 2018-12-04), it’s here.
It’s going to be a killer functionality, that will support some excellent use cases in .NET Core - especially around application plugins, extensibility and dynamic assembly generation.
Let’s have a quick look at how we can load and unload assemblies in .NET Core.
Using Roslyn C# Completion Service programmatically
I am involved in a few open source projects built around the Roslyn compiler. One of those projects is OmniSharp, which brings intellisense and C# language services to a number of editors out there, allowing them to provide for their users a rich C# code authoring experience.
Which actually brings me to the point of today’s post. Roslyn is a compiler-as-a-service that you can embed in your own app, and when you do that, you could reach into its C# language services (more specifically, CompletionService) and easily build your own C# intellisense engine.
However, this is not really documented, so I wanted to use this post to show you how you can get started with that.
dotnet-script 0.27.0 is out – with a ton of features!
It’s that time again, the new dotnet-script release, version 0.27.0 is out. It’s been 2+ months since the last release so this one is fully packed with great features.
You can get it by running dotnet tool install dotnet-script -g (if you don’t have it installed yet) or dotnet tool update dotnet-script -g (if you just need an update).
Let me quickly walk you through some of them.
No InternalsVisibleTo, no problem – bypassing C# visibility rules with Roslyn
Both the C# compiler and the CLR/CoreCLR runtimes contain a bunch of rules that are in place to save us from ourselves (and to allow us to write code without needing to fully understand ECMA-334 C# Language Specification). That said, there are times where we want to do some things that are normally not allowed, and a good example of that is reaching into reflection to execute some private or internal code.
Today I wanted to show you how to do something quite cool - how to bypass the type/member visibility rules using the Roslyn compiler. In other words, how to get access to internal and private members without needing to use reflection or something like InternalsVisibleToAttribute.
Convert null-valued results to 404 in ASP.NET Core MVC
ASP.NET Core MVC is pretty flexible in terms of how it expects you to return results from the API actions. You could return an IActionResult, which gives you certain control over the status code and the nature of the response (i.e. object or a file or a status code only and so on). You could return a concrete object instance, and the framework will serialize it to the relevant response format. Finally, you could also return the new ActionResult
Let’s have a look at what happens in the framework when you return a null object instance and how you can change that behavior.
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.
About
Hi! I'm Filip W., a cloud architect from Zürich 🇨🇭. I like Toronto Maple Leafs 🇨🇦, Rancid and quantum computing. Oh, and I love the Lowlands 🏴.
Recent Posts
- 2024/11/15, Strathweb Phi Engine - now with Safe Tensors support
- 2024/10/25, How GPT-4o-mini can be simultaneously 20x cheaper and 2x more expensive than GPT-4o
- 2024/10/14, Speech-based retrieval augmented generation (RAG) with GPT-4o Realtime API
- 2024/09/06, Using Local Phi-3 Models in AutoGen with Strathweb Phi Engine
- 2024/08/30, Building a chat app with Blazor WASM, SignalR and post-quantum end-to-end encryption
Categories
- ai (15)
- 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 (21)
- azure service bus (1)
- azure-devops (1)
- benchmark dotnet (1)
- bing maps (1)
- blazor (2)
- c plus (2)
- c-sharp (155)
- cryptography (5)
- csharp (5)
- 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 (141)
- 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 (4)
- php (1)
- q-sharp (34)
- qir (3)
- quantum computing (38)
- 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)