From 8dccbf067d4c8feeebbfe2d69b36d705728213a1 Mon Sep 17 00:00:00 2001 From: Jason Zhu Date: Tue, 22 Mar 2022 23:28:12 +1100 Subject: [PATCH] Chap02: Creating Decoupled React and ASP.NET 5 Apps -> Technical requirements (create ASP.NET Core WebAPI project) --- QandA/.gitignore | 65 ++++++++++++++++++ QandA/.vs/QandA/v16/.suo | Bin 0 -> 12800 bytes QandA/QandA.sln | 25 +++++++ QandA/QandA.v3.ncrunchsolution.user | 5 ++ .../Controllers/WeatherForecastController.cs | 39 +++++++++++ QandA/QandA/Program.cs | 26 +++++++ QandA/QandA/Properties/launchSettings.json | 31 +++++++++ QandA/QandA/QandA.csproj | 11 +++ QandA/QandA/Startup.cs | 59 ++++++++++++++++ QandA/QandA/WeatherForecast.cs | 15 ++++ QandA/QandA/appsettings.Development.json | 9 +++ QandA/QandA/appsettings.json | 10 +++ 12 files changed, 295 insertions(+) create mode 100644 QandA/.gitignore create mode 100644 QandA/.vs/QandA/v16/.suo create mode 100644 QandA/QandA.sln create mode 100644 QandA/QandA.v3.ncrunchsolution.user create mode 100644 QandA/QandA/Controllers/WeatherForecastController.cs create mode 100644 QandA/QandA/Program.cs create mode 100644 QandA/QandA/Properties/launchSettings.json create mode 100644 QandA/QandA/QandA.csproj create mode 100644 QandA/QandA/Startup.cs create mode 100644 QandA/QandA/WeatherForecast.cs create mode 100644 QandA/QandA/appsettings.Development.json create mode 100644 QandA/QandA/appsettings.json 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 0000000000000000000000000000000000000000..c820f76dd61c70f178eab6fc00ac5334f9c8c5c1 GIT binary patch literal 12800 zcmeHNON<;x8Lo8*obY zR9Dyc*I)mus^{ICkG}WoH=p>M=qqQ$ed5;b2gOHo^%J;0kllYuh)?30b+>NczRl|U zK)Bp5_mBkM2d|vJ{qo_I!1H1Qzb!E-=EOSAZE=I9T)G?*_xIR*nHMvEQKoH~Hu;$= z(|)hp+{^fTIsf$kMdiDXz47+nPycD+5j6TC;s?tTG;Tw@D4v7%#kiUi*OLb&kB7v8 z3Ce#G?RhTq+B6!a@H_a#-4rcw&=yTl1GeCUnj<(Kd_gifJ4^f;z^6`_pG1!}$TwgP?~%#P~Sq z6Cm0zi86c?^cd(M=yA}eL5Dz}0qw^X2d^QN@shX#ZExdTR_`&rsyqq(qbhUm zIn<=K`1lMSA4{7o1GfX*1?YMQme3HFl?`r*P+3N&xGO>ZXHNP#P+9ylXiME@Spn*@ zl0i!ZI9~?`wEt`1Cmd=0LL31;ZGyH@7XLKb(%x8}!^j)(JU4_ChA@RCbm0k2Ob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qw}siak#=Ei-9u?5?@$-3TMs4{9~(+FBFRHB9N5VIYG@^DH|E zYL=mI*DWw_84!YLSV}@ppf#g7l78e`c#BuH(2^Keh%vjd?Zw-56K}bm?P*pb)>1Dy zl#^pQnyHz(n!nI9{3vj38Co%>BhVx1S!PWdzHc>aQ4oY4SXA?8suxPlAN@P?{kv7( z`)9c|$1waXkK*$xwgmHGdRDnG&ms?rX^Drrc0ZB7`D@ZZvG}`*P&q4Z)D}6bPM@>7 zbl05K_fls?H#qou3J$IV*OvU*go4hE^g=qga~e|0{7>%O6<_7w;q@*`LmYkh{psUB zef!*-e}3)e!QWhbWI??9+ke0ClOu1VthqH@4m3cgw!QD1uTZA2`UfkrXOCFUP+S?}ME%{E! zwhv%0Qi#3e?UZ4dByQqvi{)NB%!(09F9}fKN_s3hW Qp74KEgth + + 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": "*" +}