Asp.Net Core CheatSheet - PDF Download Cheatsheet
Start a new project
Configuration files
Environment Variables
How to access Config data and Environmental Variables
Tag Helpers
Create a Model
Create Razor Pages and EntityFramework
Dependency Injection
Controllers - ActionResult
Building an Api
Task | CLI Command |
---|---|
Create new console application | dotnet new webapp -o aspnetcoreapp |
Run the app | cd aspnetcoreapp dotnet run |
Create local Certificate | dotnet dev-certs https --trust |
- Appsettings.json - Development
Non-Private
- Manage User Secrets - Development
Private
- project => projectName properties => Debug
1. @inject Microsoft.Extensions.Cionfiguration.IConfiguration configuration
- Use @configuration["KEY"]
1. To switch them on add ``` @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers ``` to _ViewImports.cshtml.
-
In Solution Explorer, right-click the RazorPagesMovie project > Add > New Folder. Name the folder Models.
-
Right click the Models folder. Select Add > Class. Name the class Movie and replace the contents of the Movie class with the following code:
using System;
using System.ComponentModel.DataAnnotations.Schema;
namespace RazorPagesMovie.Models
{
public class Movie
{
public int ID { get; set; }
public string Title { get; set; }
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
public decimal Price { get; set; }
}
}
In Solution Explorer, right click on the Pages folder > Add > New Folder.
Name the folder Movies
-
In Solution Explorer, right click on the
Pages/Movies folder > Add > New Scaffolded Item.
-
In the Add Scaffold dialog,
select Razor Pages using Entity Framework (CRUD) > Add.
-
Complete the Add Razor Pages using Entity Framework (CRUD) dialog:
“In the Model class drop down, select Movie (RazorPagesMovie.Models). In the Data context class row, select the + (plus) sign and accept the generated name RazorPagesMovie.Models.RazorPagesMovieContext. Select Add.”
The scaffold process creates and updates the following files:
Files created
Pages/Movies: Create, Delete, Details, Edit, Index.
Data/RazorPagesMovieContext.cs
- The scaffolding tool automatically created a
DB context
and registered it with the dependency injection container. In theStartup.ConfigureServices
services.AddDbContext<RazorPagesMovieContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("RazorPagesMovieContext")));
The main page which coordinates Entity framework is the DbContext class
Here it's called RazorPagesMovieContext
Entity set created DbSet<Movie>
which corresponds with a database table.
The connection string
comes from DbContextOptions
this is read from a config file
for example appsettings.json
Types | Helper Methods |
---|---|
ViewResult | view() |
PartialViewResult | PartialView() |
ContentResult | Content() |
RedirectResult | Redirect() |
RedirectToRouteResult | RedirectToAction() |
JsonResult | Json() |
FileResult | File() |
HttpNotFoundResult | HttpNotFound() |
EmptyResult |
[HttpGet]
public IHttpActionResult GetCustomers() {}
[HttpPost]
public IHttpActionResult CreateCustomer(CustomerDto customer) {}
[HttpPut]
public IHttpActionResult UpdateCustomer(int id, CustomerDto customer) {}
[HttpDelete]
public IHttpActionResult DeleteCustomer(int id) {}
PDF Download Cheatsheet