Giter Site home page Giter Site logo

celluj34 / dontnetcore-api-angular-template Goto Github PK

View Code? Open in Web Editor NEW

This project forked from benbaran/dontnetcore-api-angular-template

0.0 0.0 0.0 147 KB

.NET Core 2 Web API with Angular Frontend Template Project

JavaScript 4.62% C# 41.46% TypeScript 45.66% CSS 0.91% HTML 3.87% PLpgSQL 3.49%

dontnetcore-api-angular-template's Introduction

Create Initial Projects

Core - .NET Core Class Library API - .NET Core Web Api Test - .Net Core MSTest Database - Database Project

Add Swagger for API Documentation

  1. Add the NuGet Package
dotnet add package Swashbuckle.AspNetCore
  1. Setup in Startup.cs ConfigureServices
services.AddMvc();

services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});
  1. Setup in Startup.cs Configure
app.UseSwagger();

app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
  1. Add Serilog for Console and SQL Logging
dotnet add package Serilog.AspNetCore
dotnet add package Serilog.Settings.Configuration
dotnet add package Serilog.Sinks.Console
dotnet add package Serilog.Sinks.MssqlServer
  1. Delete Logging sections from Appsettings.json and Appsettings.development.json

  2. Add Serilog Configuration to Appsettings.json

"Serilog": {
    "MinimumLevel": "Information",
    "WriteTo": [
      {
        "Name": "MSSqlServer",
        "Args": {
          "connectionString": "Server=127.0.0.1;Database=Data;User Id=SERILOG;Password=password123!;",
          "tableName": "Logs",
          "autoCreateSqlTable": false
        }
      },
      {
        "Name": "Console",
        "Args": {}
      }
    ]
  1. Create SQL Table for Serilog (column names are case sensitive)
CREATE LOGIN [SERILOG] WITH PASSWORD = N'password123!', DEFAULT_LANGUAGE = [us_english];

CREATE TABLE [Logs] (

   [Id] int IDENTITY(1,1) NOT NULL,
   [Message] nvarchar(max) NULL,
   [MessageTemplate] nvarchar(max) NULL,
   [Level] nvarchar(128) NULL,
   [TimeStamp] datetime NOT NULL,
   [Exception] nvarchar(max) NULL,
   [Properties] nvarchar(max) NULL

   CONSTRAINT [PK_Logs] PRIMARY KEY CLUSTERED ([Id] ASC) 
);


CREATE USER [SERILOG] FOR LOGIN [SERILOG];

GO
GRANT INSERT
    ON OBJECT::[dbo].[Logs] TO [SERILOG]
    AS [dbo];


GO
GRANT SELECT
    ON OBJECT::[dbo].[Logs] TO [SERILOG]
    AS [dbo];
    
  1. Add Serilog to Program.cs
    public class Program
    {
        // Get the appsettings.json for the current project
        public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                    .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
                    .Build();

        public static void Main(string[] args)
        {
            // Create the logger from the appsettings.json
            Log.Logger = new LoggerConfiguration()
                .ReadFrom
                .Configuration(Configuration)
                .CreateLogger();

            try
            {
                Log.Information("Starting host.");

                CreateWebHostBuilder(args).Build().Run();
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Error starting host.");
            }
            finally
            {
                Log.Information("Shutting down host.");

                Log.CloseAndFlush();
            }

            
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            .UseSerilog()
            .UseStartup<Startup>();
    }
}
  1. Configure CORS in Startup.cs Configure
app.UseCors(c => c.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
  1. Add Dapper ORM to Core Project
dotnet add package Dapper

EntityBase

All items will have an Id and DateTimeCreated property, this will be inhe=rited from the EntityBase class. Additionally, overiding the ToString() fungtion to return a JSON formatted string makes reading test results easier.

dotnet add package NewtonSoft.Json

    public class EntityBase
    {
        public Guid Id { get; set; }

        public DateTime CreateDateTime { get; set; }

        public override string ToString()
        {
            return JsonConvert.SerializeObject(this, Formatting.Indented);
        }
    }

Creating the Angular Application

npm install -g @angular/cli@latest

cd C:\Git\dontnetcore-api-angular-template

ng new template --routing --directory web

###References

  1. https://github.com/domaindrivendev/Swashbuckle.AspNetCore
  2. https://github.com/serilog/
  3. http://hamidmosalla.com/2018/02/15/asp-net-core-2-logging-with-serilog-and-microsoft-sql-server-sink/
  4. https://code-maze.com/net-core-web-development-part1/
  5. https://code-maze.com/net-core-web-development-part2/
  6. https://code-maze.com/net-core-web-development-part3/
  7. https://code-maze.com/net-core-web-development-part4/

dontnetcore-api-angular-template's People

Contributors

benbaran avatar benbaran-journeycare avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.