commit 8dccbf067d4c8feeebbfe2d69b36d705728213a1 Author: Jason Zhu Date: Tue Mar 22 23:28:12 2022 +1100 Chap02: Creating Decoupled React and ASP.NET 5 Apps -> Technical requirements (create ASP.NET Core WebAPI project) diff --git a/QandA/.gitignore b/QandA/.gitignore new file mode 100644 index 0000000..f2f5b95 --- /dev/null +++ b/QandA/.gitignore @@ -0,0 +1,65 @@ +# File created using '.gitignore Generator' for Visual Studio Code: https://bit.ly/vscode-gig + +# Created by https://www.toptal.com/developers/gitignore/api/windows,visualstudiocode,dotnetcore +# Edit at https://www.toptal.com/developers/gitignore?templates=windows,visualstudiocode,dotnetcore + +### DotnetCore ### +# .NET Core build folders +bin/ +obj/ + +# Common node modules locations +/node_modules +/wwwroot/node_modules + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Local History for Visual Studio Code +.history/ + +# Built Visual Studio Code Extensions +*.vsix + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history +.ionide + +# Support for Project snippet scope + +### Windows ### +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# End of https://www.toptal.com/developers/gitignore/api/windows,visualstudiocode,dotnetcore + +# Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option) + diff --git a/QandA/.vs/QandA/v16/.suo b/QandA/.vs/QandA/v16/.suo new file mode 100644 index 0000000..c820f76 Binary files /dev/null and b/QandA/.vs/QandA/v16/.suo differ diff --git a/QandA/QandA.sln b/QandA/QandA.sln new file mode 100644 index 0000000..d65943f --- /dev/null +++ b/QandA/QandA.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.32228.343 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QandA", "QandA\QandA.csproj", "{2C29872B-C119-4B2B-B6FB-B715978DF6C9}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {2C29872B-C119-4B2B-B6FB-B715978DF6C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2C29872B-C119-4B2B-B6FB-B715978DF6C9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2C29872B-C119-4B2B-B6FB-B715978DF6C9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2C29872B-C119-4B2B-B6FB-B715978DF6C9}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {AF070AE9-57CB-4C03-9176-B577D95D6799} + EndGlobalSection +EndGlobal diff --git a/QandA/QandA.v3.ncrunchsolution.user b/QandA/QandA.v3.ncrunchsolution.user new file mode 100644 index 0000000..b82f7a5 --- /dev/null +++ b/QandA/QandA.v3.ncrunchsolution.user @@ -0,0 +1,5 @@ + + + Run all tests automatically [Global] + + \ No newline at end of file diff --git a/QandA/QandA/Controllers/WeatherForecastController.cs b/QandA/QandA/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..59f6e13 --- /dev/null +++ b/QandA/QandA/Controllers/WeatherForecastController.cs @@ -0,0 +1,39 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace QandA.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + private readonly ILogger _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + [HttpGet] + public IEnumerable Get() + { + var rng = new Random(); + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateTime.Now.AddDays(index), + TemperatureC = rng.Next(-20, 55), + Summary = Summaries[rng.Next(Summaries.Length)] + }) + .ToArray(); + } + } +} diff --git a/QandA/QandA/Program.cs b/QandA/QandA/Program.cs new file mode 100644 index 0000000..5802a02 --- /dev/null +++ b/QandA/QandA/Program.cs @@ -0,0 +1,26 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace QandA +{ + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); + } +} diff --git a/QandA/QandA/Properties/launchSettings.json b/QandA/QandA/Properties/launchSettings.json new file mode 100644 index 0000000..e074873 --- /dev/null +++ b/QandA/QandA/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:15489", + "sslPort": 44322 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "QandA": { + "commandName": "Project", + "dotnetRunMessages": "true", + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:5001;http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/QandA/QandA/QandA.csproj b/QandA/QandA/QandA.csproj new file mode 100644 index 0000000..2884716 --- /dev/null +++ b/QandA/QandA/QandA.csproj @@ -0,0 +1,11 @@ + + + + net5.0 + + + + + + + diff --git a/QandA/QandA/Startup.cs b/QandA/QandA/Startup.cs new file mode 100644 index 0000000..4555b4a --- /dev/null +++ b/QandA/QandA/Startup.cs @@ -0,0 +1,59 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.HttpsPolicy; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Microsoft.OpenApi.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace QandA +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + + services.AddControllers(); + services.AddSwaggerGen(c => + { + c.SwaggerDoc("v1", new OpenApiInfo { Title = "QandA", Version = "v1" }); + }); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + app.UseSwagger(); + app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "QandA v1")); + } + + app.UseHttpsRedirection(); + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + } + } +} diff --git a/QandA/QandA/WeatherForecast.cs b/QandA/QandA/WeatherForecast.cs new file mode 100644 index 0000000..de304b1 --- /dev/null +++ b/QandA/QandA/WeatherForecast.cs @@ -0,0 +1,15 @@ +using System; + +namespace QandA +{ + public class WeatherForecast + { + public DateTime Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string Summary { get; set; } + } +} diff --git a/QandA/QandA/appsettings.Development.json b/QandA/QandA/appsettings.Development.json new file mode 100644 index 0000000..8983e0f --- /dev/null +++ b/QandA/QandA/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/QandA/QandA/appsettings.json b/QandA/QandA/appsettings.json new file mode 100644 index 0000000..d9d9a9b --- /dev/null +++ b/QandA/QandA/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +}