The 201 on Building Web API with ASP.NET Core MVC

Happy to announce the availability of my new book “The 201 on Building Web API with ASP.NET Core MVC” in Amazon.

The 201 on Building Web API with ASP.NET Core MVC

The 201 on Building Web API with ASP.NET Core MVC, is an intermediate-level guide on using ASP.NET Core and ASP.NET Core MVC’s extensibility points to solve problems, as you build a web API using ASP.NET Core MVC. ASP.NET Core MVC is a powerful framework and out of box, it gets you far. But there are scenarios where you will want to go even farther to get things done. If you are one of those who has gone through the framework 101 and has a need to see farther, this book is for you. This book employs a problem-solution format so that you can read the topics that are of interest to you, in any order.

  • Action constraints
  • Action results
  • Application model conventions
  • Application parts
  • Authentication middleware
  • Batching messages
  • Binding source value providers
  • Filter factories
  • Filter providers
  • Formatters
  • Localization
  • Loggers and logger Providers
  • Middleware
  • Model binders
  • Request validation
  • Resource, Result, Exception, Action Filters
  • Value providers


1     HTTP 101
1.1  Exposing API over Web
1.2  Returning a Resource Representation
1.3  Querying for Resources
1.4  Creating/Replacing a Resource
1.5  Adding a New Resource to a Collection
1.6  Deleting a Resource
1.7  Using POST to Tunnel other HTTP Methods

2.1  Using Configuration for Action Selection
2.2  Using Convention for Action Selection
2.3  Using a Controller Resource
2.4  Designing Hierarchical URI
2.5  Controlling Route Template Parameter Matching
2.6  Selecting Action Based on Request Content Type
2.7  Disambiguating between Action Methods
2.8  Using Plugin-based Design
2.9  Differentiating 404 – Not Found

3.1  Binding Different Parts of the Request Message
3.2  Filling in the Missing Part of the Model
3.3  Providing a Value to Model Binder on Priority
3.4  Handling XML Requests
3.5  XML Input Formatting on a Per-Request Basis
3.6  Binding the Model from Nonstandard Payloads
3.7  Using Factory Pattern for Model Binding
3.8  Validating Requests
3.9  Using Matrix Parameters

4.1  Enabling Content Negotiation
4.2  Disabling Content Negotiation Selectively
4.3  Selecting Content Type on a Per-Request Basis
4.4  Detecting Modifications to the Response Body
4.5  XML Output Formatting on a Per-Request Basis

5.1  Preventing Action Method from Running
5.2  Preventing Duplicate HTTP POSTs
5.3  Handling Exceptions
5.4  Specifying Order for the Filters
5.5  Creating a Dynamic Filter Pipeline
5.6  Overriding Filters

6.1  Logging Request Body for All Requests
6.2  Logging Request Body Selectively
6.3  Logging Response Body
6.4  Structured Logging
6.5  Logging to Amazon DynamoDB
6.6  Logging Additional Fields in JSON Request Body

7.1  Injecting Dependencies
7.2  Resolving Dependencies by Convention
7.3  Proxying Dependencies
7.4  Sharing Data among ASP.NET Components
7.5  Sharing Context among ASP.NET Components
7.6  Sharing Data across Different Application Layers

8.1  Returning a Partial Resource Representation
8.2  Using a Composite Resource
8.3  Caching Responses
8.4  Returning a Representation Only on Changes
8.5  Using a Binary Message Format
8.6  Streaming JSON Response
8.7  Going Asynchronous
8.8  Batching Request Messages

9.1  Sending Custom Response Headers
9.2  Implementing Custom Authentication
9.3  Creating Middleware versus Filter
9.4  Establishing Current Culture
9.5  Localizing Based on Current Culture
9.6  Using ASP.NET Core MVC Core

Specifying Environment from the Command Line in ASP.NET Core

ASP.NET Core picks up the environment from the environment variable ASPNETCORE_ENVIRONMENT. If you want to set it from Visual Studio, you can do so by double clicking “Properties” in Solution Explorer > Debug > Environment Variables. Just add an entry with a name of “ASPNETCORE_ENVIRONMENT” and a value desired.

If you want to set the environment from the command line, you have to modify the Main method like this.

public static void Main(string[] args)
    var config = new ConfigurationBuilder().AddCommandLine(args).Build();

    var host = new WebHostBuilder()


With that, you can pass the environment, like so.

dotnet run --environment "UAT"

You can also specify server URL using command line.

dotnet run --environment UAT --server.urls http://*:5001

That will set the environment to “UAT” and let Kestrel use port 5001.

Web API Model Binding in ASP.NET MVC 6 (ASP.NET 5)

In ASP.NET 5, MVC and Web API have been merged into a single framework called MVC 6. If you are deep into Web API and MVC in the previous versions of ASP.NET, like me, it will take some time to get used to how binding works in MVC 6. Especially, if you are more into Web API in the recent past than MVC, like me, you will actually feel sad to see that the things you are so used to with Web API are all gone and it is different now : (.
Continue reading

Using Thinktecture Hawk Katana Authentication Middleware with ASP.NET 5.0 (ASP.NET MVC 6)

In this post, I have covered Katana middleware versus ASP.NET 5.0 middleware. Calling a normal Katana middleware that accepts AppFunc from ASP.NET 5.0 pipeline is not that difficult. You can just use the UseOwin extension method on IApplicationBuilder, like so.

app.UseOwin(addToPipeline =>
    addToPipeline(next =>
        return new MyNormalKatanaMiddleware(next).Invoke;

Continue reading

ASP.NET vNext Middleware for Hawk Authentication – An Experiment

Thinktecture Identity model has support for Eran Hammer’s Hawk authentication in the form of OWIN middleware. With ASP.NET vNext introducing a new but very similar middleware concept (changes only on the API surface and OWIN middleware can be used as-is in ASP.NET VNext apps), I took the opportunity to completely rewrite the middleware. As ASP.NET vNext is being actively developed, there could be changes and this middleware will change to react to those changes. Hence, at this point, this middleware is just for review only. Since Linus had said “Talk is cheap. Show me the code.”, I keep this blog post very short and just link to github. Your feedback is welcome.