Practical samples of aspnet core projects you can use
Latest commit 8ba1245 Dec 29, 2016 @dodyg Improve labeling
Failed to load latest commit information.
caching-2 Use wildcard for dotnet watch version. Now it works. Nov 18, 2016
caching-3 Clean up code Nov 18, 2016
caching-4 Clean up code Nov 18, 2016
caching Clean up namespace Nov 18, 2016
configuration-environment-variables Take .net461 dep Nov 18, 2016
configuration-ini-options Upgrade to DOTNET Core 1.1 Nov 18, 2016
configuration-ini Take .net461 dep Nov 18, 2016
configuration-options Upgrade to .NET Core 1.1 Nov 20, 2016
configuration-xml-options Upgrade to .NET Core 1.1 Nov 20, 2016
configuration-xml Upgrade to .NET Core 1.1 Nov 20, 2016
configuration Update to .NET 1.1 and fix errors Nov 18, 2016
dependency-injection-1 Remove unused using Nov 20, 2016
dependency-injection-3 Update to .NET Core 1.1 Nov 20, 2016
features-connection Update to .NET Core 1.1 Nov 20, 2016
features-request-culture Update to .NET Core 1.1 Nov 20, 2016
features-server-addresses Update to .NET Core 1.1 Nov 20, 2016
features-server-custom-override Update to .NET Core 1.1 Nov 20, 2016
features-server-custom Update to .NET Core 1.1 Nov 20, 2016
features-server-request remove unused using Nov 20, 2016
features-session Update Nov 23, 2016
hello-world-env-development Update to .NET Core 1.1 Nov 20, 2016
hello-world-startup-basic Update to .NET Core 1.1 Nov 20, 2016
hello-world-with-IApplicationLifetime Update to .NET Core 1.1 Nov 20, 2016
hello-world-with-IHostingEnvironment Update to .NET Core 1.1 Nov 20, 2016
hello-world-with-cookies-2 Complete a sample on how to remove cookie Dec 14, 2016
hello-world-with-cookies Update to .NET Core 1.1 Nov 20, 2016
hello-world-with-header Fix launch.js Dec 14, 2016
hello-world-with-logging-filtered Update to .NET Core 1.1 Nov 20, 2016
hello-world-with-logging Update to .NET Core 1.1 Nov 20, 2016
hello-world-with-middleware Update to .NET Core 1.1 Nov 20, 2016
hello-world-with-reload Update to .NET Core 1.1 Nov 20, 2016
localization-2 Add en-US resources Nov 30, 2016
localization-3 Add a sample for culture and UI culture Dec 5, 2016
localization-4 Create an example on how to use CookieRequestCultureProvider Dec 11, 2016
localization Update the dotnet watch tool Nov 30, 2016
markdown-server-middleware Update to .NET Core 1.1 Nov 20, 2016
markdown-server Update to .NET Core 1.1 Nov 20, 2016
middleware-1 Update to .NET Core 1.1 Nov 20, 2016
mvc-accept-xml Update to .NET Core 1.1 Nov 20, 2016
password-hasher Update to .NET Core 1.1 Nov 20, 2016
rewrite-2 Rewrite 2 shows value capture Dec 24, 2016
rewrite Tighten the example Dec 24, 2016
routing-10 Update to .NET Core 1.1 Nov 20, 2016
routing-2 Update to .NET Core 1.1 Nov 20, 2016
routing-3 Update to .NET Core 1.1 Nov 20, 2016
routing-4 Update to .NET Core 1.1 Nov 20, 2016
routing-5 Still not working but .. Nov 30, 2016
routing-6 Update to .NET Core 1.1 Nov 20, 2016
routing-7 Update to .NET Core 1.1 Nov 20, 2016
routing-8 Update to .NET Core 1.1 Nov 20, 2016
routing-9 Update to .NET Core 1.1 Nov 20, 2016
routing Update to .NET Core 1.1 Nov 20, 2016
serve-static-files Update to .NET Core 1.1 Nov 20, 2016
.gitignore Initial project Jul 27, 2016
LICENSE Initial commit Jul 27, 2016 Improve labeling Dec 29, 2016

51 samples for aspnetcore fundamentals (updated daily)


The goal of this project is to enable .NET programmers to learn the new ASP.NET Core stack from the ground up directly from code. I will not address ASP.NET Core MVC in this project (at least not yet).

Every sample is designed specifically to demonstrate a single idea. We will go wide and deep to the nitty gritty of ASP.NET Core stack. Enjoy the ride!

WARNING: DO NOT UPGRADE / INSTALL .NET Core Tools MSBuild “alpha” right now. It breaks a lot of stuffs.

DO: Do upgrade to .NET Core 1.1 (and SDK 1.0.0 Preview2.1). It contains a lot of bug fixes and new features.

Hints: If you type dotnet --version, it should return the following string 1.0.0-preview2-1-003177.

Some of the samples you see here involve mixed projects (net451) that will run only in Windows. For many .NET developers, full framework is the reality for forseeable future. We are not going to port multi-year production systems to run on Linux. We want to improve the creaky .NET MVC 2.0 that we have lying around and bring it up to speed to aspnetcore MVC.

All these projects require the following dependencies

   "Microsoft.AspNetCore.Hosting" : "1.1.0-*"

This dependency pulls its own dependencies which you can check at project.lock.json. This allows us to not explicitly specify some dependencies ourselves.

If a sample require additional dependencies, I will list them.

I highly recommend using Visual Studio Code to play around with these samples but it is not required.

To run these samples, simply open your command line console, go to each folder and execute dotnet restore and then continue with dotnet watch run.

  • Hello World (11)

    • Hello World with reload

      Setup your most basic web app and enable the change+refresh development experience.

      We are using IApplicationBuilder Run, an extension method for adding terminal middleware.

    • Hello World with startup basic

      This project contains all the available services available in Startup class constructor, ConfigureServices and Configure methods.

    • Environmental settings

      Set your application environment to Development or Production or other mode directly from code.

    • Console logging

      Setup a basic logging in your app and show it to console.

      We add the following dependencies "Microsoft.Extensions.Logging": "1.1.0" and "Microsoft.Extensions.Logging.Console": "1.1.0"

      We are using IApplicationBuilder Run, an extension method for adding terminal middleware.

    • Console logging - without framework log messages

      Filter out frameworking logging from your log output. Without filtering, logging can get very annoying becuase the framework produces a lot of messages.

    • Hello World with middlewares

      ASPNetCore is built on top of pipelines of functions called middleware.

      We are using IApplicationBuilder Use, an extension method for adding middleware and IApplicationBuilder Run.

    • IApplicationLifetime

      Respond to application startup and shutdown.

      We are using IApplicationLifetime that trigger events during application startup and shutdown.

    • IHostingEnvironment

      IHostingEnvironment is available at Startup constuctor and Startup.Configure. This sample shows all the properties available in this interface.

    • Cookies

      Simply read and write cookies.

    • Removing cookies

      Simply demonstrate on how to remove cookies.

    • Adding HTTP Response Header

      Demonstrate on how to add a response header and where is allowed place to do it.

  • Routing (9)

    We take dependency on "Microsoft.AspNetCore.Routing" : "1.1.0-*" to enable routing facilities in your aspnetcore apps. There are several samples to illuminate this powerful library.

    • Router

      A single route handler that handles every path request.

    • Router 2

      Two route handler, one for home page (/) and the other takes the rest of the request using asterisk (*) in the url template.

    • Router 3

      We are exploring default handler - this is the entry point to create your own framework.

    • Router 4

      We are mixing optional route parameter, route parameter with default value and default handler.

    • [Router 5]

      This is still broken. I am trying to figure out how to do nested routing. Wish me luck!

    • Router 6

      We are building a template route segment by segment and parts by parts, oldskool. We are using TemplateMatcher, TemplateSegment and TemplatePart.

      Hold your mask, we are going deep.

    • Router 7

      We are creating a routing template with two segments, one with Literal part and the other Parameter part, e.g, "/page/{*title}"

    • Router 8

      We are creating a routing template with one segment consisted of two parts, one Literal and one Parameter, e.g. "/page{*title}". Note the difference between this example and Router 7.

    • Router 9

      I am still trying to determine whether TemplateMatcher uses the InlineConstraint information.

      Update: No, TemplateMatcher does not run constraints. #362

    • Router 10

      We have been building a RouteTemplate manually using TemplateSegment and TemplatePart. In this example we are using TemplateParser to build the RouteTemplate using string.

  • Middleware (1)

    We will explore all aspect of middleware building in this section. There is no extra dependency taken other than Kestrel and dotnet watch.

    • Middleware 1

      This example shows how to pass information from one middleware to another using HttpContext.Items.

  • Features (7)

    Features are collection of objects you can obtain from the framework at runtime that serve different purposes.

    • Server Addresses Feature

      Use this Feature to obtain a list of urls that your app is responding to.

    • Request Feature

      Obtain details of a current request. It has some similarity to HttpContext.Request. They are not equal. HttpContext.Request has more properties.

    • Connection Feature

      Use IHttpConnectionFeature interface to obtain local ip/port and remote ip/port.

    • Custom Feature

      Create your own custom Feature and pass it along from a middleware.

    • Custom Feature - Override

      Shows how you can replace an implementation of a Feature with another within the request pipeline.

    • Request Culture Feature

      Use this feature to detect the culture of a web request through IRequestCultureFeature. It needs the following dependency "Microsoft.AspNetCore.Localization": "1.1.0".

    • Session Feature

      Use session within your middlewares. This sample shows a basic usage of in memory session. It needs the following dependency '"Microsoft.AspNetCore.Session" : "1.1.0-"and"Microsoft.Extensions.Caching.Memory" : "1.1.0-"`.

  • Dependency Injection (2)

    aspnetcore lives and die by DI. It relies on Microsoft.Extensions.DependencyInjection library. There is no need to put this dependency in your project.json explicitly because aspnetcore already has this package as its own dependency.

    • Dependency Injection 1 - The basic

      Demonstrate the three lifetime registrations for the out of the box DI functionality: singleton (one and only forever), scoped (one in every request) and transient (new everytime).

    • Dependency Injection 3 - Easy registration

      Register all objects configured by classes that implements a specific interface (IBootstrap in this example). This is useful when you have large amount of classes in your project that needs registration. You can register them near where they are (usually in the same folder) instead of registering them somewhere in a giant registration function.

      Note: example 2 is forthcoming. The inspiration has not arrived yet.

  • In Memory Caching (a.k.a local cache) (4)

    These samples depends on Microsoft.Extensions.Caching.Memory library. Please add this dependency to your project.json.

    • Caching - Absolute/Sliding expiration

      This is the most basic caching you can use either by setting absolute or sliding expiration for your cache. Absolute expiration will remove your cache at a certain point in the future. Sliding expiration will remove your cache after period of inactivity.

    • Caching 2 - File dependency

      Add file dependency to your caching so when the file changes, your cache expires.

      You need to put the cache file in your project.json so it gets copied over, e.g.

      "buildOptions": { "emitEntryPoint": true, "copyToOutput": ["cache-file.txt"] }

      Note: example 1 is forthcoming. The inspiration has not arrived yet.

    • Caching 3 - Cache removal event

      Register callback when a cached value is removed.

    • Caching 4 - CancellationChangeToken dependency

      Bind several cache entries to a single dependency that you can reset manually.

  • Configuration (7)

    This section is all about configuration, from memory configuration to INI, JSON and XML.

    • Configuration

      This is the 'hello world' of configuration. Just use a memory based configuration and read/write values to/from it.

    • Configuration - Options

      Use IOptions at the most basic.

    • Configuration - Environment variables

      Load environment variables and display all of them.

    • Configuration - INI file

      Read from INI file. It requires taking a new dependency, "Microsoft.Extensions.Configuration.INI" : "1.1.0".

    • Configuration - INI file - Options

      Read from INI file (with nested keys) and IOptions. It requires taking two new dependencies, "Microsoft.Extensions.Configuration.INI" : "1.1.0" and "Microsoft.Extensions.Options.ConfigurationExtensions" : "1.1.0".

    • Configuration - XML file

      Read from XML file. It requires taking a new dependency, "Microsoft.Extensions.Configuration.Xml" : "1.1.0".

      Note: This Xml Configuration provider does not support repeated element.

      The following configuration settings will break:

        <add key="webpages:Version" value="" />
        <add key="webpages:Enabled" value="false" />

      On the other hand you can get unlimited nested elements and also attributes.

    • Configuration - XML file - Options

      Read from XML file and use IOptions. It requires taking two new dependencies, "Microsoft.Extensions.Configuration.Xml" : "1.1.0" and "Microsoft.Extensions.Options.ConfigurationExtensions" : "1.1.0".

  • Localization and Globalization (4)

    This section is all about languages, culture, etc.

    • Localization

      Shows the most basic use of localization using a resource file. This sample only supports French language (because we are fancy). It needs the following dependency "Microsoft.AspNetCore.Localization": "1.1.0" and "Microsoft.Extensions.Localization": "1.1.0".

    • Localization - 2

      We build upon the previous sample and demonstrate how to switch request culture via query string using the built in QueryStringRequestCultureProvider. This sample supports English and French.

    • Localization - 3

      Demonstrate the difference between Culture and UI Culture.

    • Localization - 4

      Demonstrate how to switch request culture via cookie using the built in CookieRequestCultureProvider. This sample supports English and French.

  • URL Rewriting (2)

    This section explore the dark arts of URL Rewriting

    • Rewrite

      Shows the most basic of URL rewriting which will redirect anything to the home page "/". It requires "Microsoft.AspNetCore.Rewrite" : "1.0.0-*" and "Microsoft.AspNetCore.Routing" : "1.1.0-*" dependencies.

      If you have used routing yet, I recommend of checking out the routing examples.

    • Rewrite - 2

      Redirect anything with an extension e.g. about-us.html or welcome.aspx to home page (/). It also shows how to capture the matched regex values.

  • Misc (4)

    • Serve static files

      Simply serve static files (html, css, images, etc).

      This additional dependency is required to enable the functionality "Microsoft.AspNetCore.StaticFiles": "1.1.0".

      There are two static files being served in this project, index.html and hello.css. They are stored under wwwroot folder, which is the default folder location for this library.

      To access them you have to refer them directly e.g. localhost:5000/index.html and localhost:5000/hello.css.

    • Markdown server

      Serve markdown file as html file. You will see how you can create useful app using a few basic facilities in aspnetcore.

      We take "CommonMark.Net" : "0.13.4" as dependency.

    • Markdown server - implemented as middleware component

      Serve markdown file as html file. It has the same exact functionality as Markdown server but implemented using middleware component.

      We take "CommonMark.Net" : "0.13.4" as dependency.

      Check out the documentation on how to write your own middleware.

    • Password Hasher server

      Give it a string and it will generate a secure hash for you, e.g. localhost:5000?password=mypassword.

      We add dependency "Microsoft.AspNetCore.Identity": "1.1.0-*" to enable this functionality.

Other resources

These are other aspnetcore resources with code samples

Contributor Guidelines

  • Put all the code inside Program.cs. It makes it easier for casual users to read the code online and learn something. Sometimes it is too cumbersome to chase down types using browser.
  • Keep your sample very simple and specific. Try to minimise the amount of concept that people need to know in order to understand your code.
  • There is no sample that is too small. If it shows one single interesting and useful knoweldge, add it in.
  • When you are ready, update this document and add the link to the project with a paragraph or two. Do not forget to increment the sample count at the beginning of this document.