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.

Continue Reading


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.

Continue Reading


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<T> which allows you to mix both of the previous approaches in a single method, giving you the best of both worlds.

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.

Continue Reading


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.

Continue Reading



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.

Continue Reading


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.

Continue Reading


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 <PackageReference /> entries to define their NuGet dependencies).

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.

Continue Reading


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.

Continue Reading


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.

Continue Reading


Pages:123...21»