Skip to content

ASP.NET Core app

Using Yeoman to generate a ASP.NET Core app from a template

Install Node.js and npm

To get started with Yeoman, install Node.js. The installer includes Node.js and npm.

  • for Mac OS X
brew install node
  • for Windows OS
choco install nodejs

Install Yeoman and Bower

npm install -g yo

npm install -g bower

Install generator-aspnet

npm install -g generator-aspnet

Run with

yo aspnet

See also: Building Projects with Yeoman on

Optionaly install the yeoman extension in Visual Studio Code


Onion Architecture In ASP.NET Core MVC

Example of a Web API built on ASP.NET Core

Routing Examples

public class TestController : Controller

// /hello
public IActionResult Hello() => Ok("Hello");

// /hi only GET method
[Route("/hi")] [HttpGet]
public IActionResult Hi() => Ok("Hi");

//Alternative for previous
public IActionResult Hi() => Ok("Hi");

//Route prefix
public class TestController2 : Controller
//You can have multiple routes on an action
[Route("")] // /test
[Route("hello")]  // /test/hello
public IActionResult Hello() => Ok("Hello");

// Maps to both: // /test/hi, and: // /hi
[Route("/hi")] // Overrides the prefix with /, you can also use ~/
public IActionResult Hi() => Ok("Hi");

// /test/greet/Joon -> maps Joon to the name parameter
public IActionResult Greet(string name) => Ok($"Hello {name}!");

//Parameters can be optional
// /test/greetopt -> name == null
// /test/greetopt/Joon -> name == Joon
public IActionResult GreetOptional(string name) => Ok(name == null ? "No name" : "Hi!");

// You can use [controller], [action], and [area] to create generic templates
public class MyController : Controller
// /my/info
public IActionResult Info() => Ok("Info");
// /my/i
public IActionResult Info2() => Ok("Info2"); }

public class SelectionController : Controller
//You can use constraints to influence route selection
//Do not use for validation!
// /users/123
public IActionResult Int(int id) => Ok($"Looked up user id {id}");

// /users/joonas
public IActionResult String(string name) => Ok($"User name {name}");