From cfc888d584aa756f45e37a9e7a27c742f51e8248 Mon Sep 17 00:00:00 2001 From: ACER Date: Mon, 11 Sep 2023 16:17:30 +0700 Subject: [PATCH] Add project files. --- DemoSolution.sln | 31 +++++++++ .../Controllers/WeatherForecastController.cs | 33 ++++++++++ OwinDemo/OwinDemo.csproj | 14 +++++ OwinDemo/Program.cs | 57 +++++++++++++++++ OwinDemo/Properties/launchSettings.json | 31 +++++++++ OwinDemo/WeatherForecast.cs | 13 ++++ OwinDemo/appsettings.Development.json | 8 +++ OwinDemo/appsettings.json | 9 +++ .../Controllers/ValueController.cs | 39 ++++++++++++ .../Controllers/WeatherForecastController.cs | 63 +++++++++++++++++++ RESTfulServicesDemo/Program.cs | 46 ++++++++++++++ .../Properties/launchSettings.json | 31 +++++++++ .../RESTfulServicesDemo.csproj | 13 ++++ RESTfulServicesDemo/WeatherForecast.cs | 13 ++++ .../appsettings.Development.json | 8 +++ RESTfulServicesDemo/appsettings.json | 9 +++ 16 files changed, 418 insertions(+) create mode 100644 DemoSolution.sln create mode 100644 OwinDemo/Controllers/WeatherForecastController.cs create mode 100644 OwinDemo/OwinDemo.csproj create mode 100644 OwinDemo/Program.cs create mode 100644 OwinDemo/Properties/launchSettings.json create mode 100644 OwinDemo/WeatherForecast.cs create mode 100644 OwinDemo/appsettings.Development.json create mode 100644 OwinDemo/appsettings.json create mode 100644 RESTfulServicesDemo/Controllers/ValueController.cs create mode 100644 RESTfulServicesDemo/Controllers/WeatherForecastController.cs create mode 100644 RESTfulServicesDemo/Program.cs create mode 100644 RESTfulServicesDemo/Properties/launchSettings.json create mode 100644 RESTfulServicesDemo/RESTfulServicesDemo.csproj create mode 100644 RESTfulServicesDemo/WeatherForecast.cs create mode 100644 RESTfulServicesDemo/appsettings.Development.json create mode 100644 RESTfulServicesDemo/appsettings.json diff --git a/DemoSolution.sln b/DemoSolution.sln new file mode 100644 index 0000000..e8766c0 --- /dev/null +++ b/DemoSolution.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.7.34024.191 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OwinDemo", "OwinDemo\OwinDemo.csproj", "{3DD6938D-773D-4B0E-9C84-F3569351ADAD}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RESTfulServicesDemo", "RESTfulServicesDemo\RESTfulServicesDemo.csproj", "{15081B66-788C-44EF-919B-02D82D853FC5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3DD6938D-773D-4B0E-9C84-F3569351ADAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3DD6938D-773D-4B0E-9C84-F3569351ADAD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3DD6938D-773D-4B0E-9C84-F3569351ADAD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3DD6938D-773D-4B0E-9C84-F3569351ADAD}.Release|Any CPU.Build.0 = Release|Any CPU + {15081B66-788C-44EF-919B-02D82D853FC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {15081B66-788C-44EF-919B-02D82D853FC5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {15081B66-788C-44EF-919B-02D82D853FC5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {15081B66-788C-44EF-919B-02D82D853FC5}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B748D3D8-2126-466C-8490-A6E9000FC5AB} + EndGlobalSection +EndGlobal diff --git a/OwinDemo/Controllers/WeatherForecastController.cs b/OwinDemo/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..5bfbeba --- /dev/null +++ b/OwinDemo/Controllers/WeatherForecastController.cs @@ -0,0 +1,33 @@ +using Microsoft.AspNetCore.Mvc; + +namespace OwinDemo.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(Name = "GetWeatherForecast")] + public IEnumerable Get() + { + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateTime.Now.AddDays(index), + TemperatureC = Random.Shared.Next(-20, 55), + Summary = Summaries[Random.Shared.Next(Summaries.Length)] + }) + .ToArray(); + } + } +} \ No newline at end of file diff --git a/OwinDemo/OwinDemo.csproj b/OwinDemo/OwinDemo.csproj new file mode 100644 index 0000000..2ff2bb7 --- /dev/null +++ b/OwinDemo/OwinDemo.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/OwinDemo/Program.cs b/OwinDemo/Program.cs new file mode 100644 index 0000000..bc727c5 --- /dev/null +++ b/OwinDemo/Program.cs @@ -0,0 +1,57 @@ +using System.Globalization; +using System.Net.WebSockets; +using System.Text; + +namespace OwinDemo +{ + public class Program + { + public static void Main(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + + // Add services to the container. + + builder.Services.AddControllers(); + // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle + builder.Services.AddEndpointsApiExplorer(); + builder.Services.AddSwaggerGen(); + + var app = builder.Build(); + + // Configure the HTTP request pipeline. + if (app.Environment.IsDevelopment()) + { + //app.UseSwagger(); + //app.UseSwaggerUI(); + app.UseOwin(pipeline => + { + pipeline(next => OwinHello); + }); + } + + app.UseHttpsRedirection(); + + app.UseAuthorization(); + + + app.MapControllers(); + + app.Run(); + } + public static Task OwinHello(IDictionary environment) + { + string responseText = "Hello World via OWIN"; + byte[] responseBytes = Encoding.UTF8.GetBytes(responseText); + + // OWIN Environment Keys: https://owin.org/spec/spec/owin-1.0.0.html + var responseStream = (Stream)environment["owin.ResponseBody"]; + var responseHeaders = (IDictionary)environment["owin.ResponseHeaders"]; + + responseHeaders["Content-Length"] = new string[] { responseBytes.Length.ToString(CultureInfo.InvariantCulture) }; + responseHeaders["Content-Type"] = new string[] { "text/plain" }; + + return responseStream.WriteAsync(responseBytes, 0, responseBytes.Length); + } + } +} \ No newline at end of file diff --git a/OwinDemo/Properties/launchSettings.json b/OwinDemo/Properties/launchSettings.json new file mode 100644 index 0000000..d109b11 --- /dev/null +++ b/OwinDemo/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:24718", + "sslPort": 44337 + } + }, + "profiles": { + "OwinDemo": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7188;http://localhost:5269", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/OwinDemo/WeatherForecast.cs b/OwinDemo/WeatherForecast.cs new file mode 100644 index 0000000..5c02289 --- /dev/null +++ b/OwinDemo/WeatherForecast.cs @@ -0,0 +1,13 @@ +namespace OwinDemo +{ + 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; } + } +} \ No newline at end of file diff --git a/OwinDemo/appsettings.Development.json b/OwinDemo/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/OwinDemo/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/OwinDemo/appsettings.json b/OwinDemo/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/OwinDemo/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/RESTfulServicesDemo/Controllers/ValueController.cs b/RESTfulServicesDemo/Controllers/ValueController.cs new file mode 100644 index 0000000..a0ce924 --- /dev/null +++ b/RESTfulServicesDemo/Controllers/ValueController.cs @@ -0,0 +1,39 @@ +using Microsoft.AspNetCore.Mvc; + +namespace RESTfulServicesDemo.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class ValueController : Controller + { + [HttpGet] + public IActionResult Get() + { + return Ok(new string[] {"value1", "value2"}); + } + + [HttpGet("one")] + public IEnumerable Get1() + { + return new string[] { "value1", "value2" }; + } + + [HttpGet("two")] + public ActionResult> Get2() + { + return new string[] { "value1", "value2" }; + } + + [HttpGet("three")] + public string[] Get3() + { + return new string[] { "value1", "value2" }; + } + + [HttpGet("four")] + public IActionResult Get4() + { + return new JsonResult(new string[] { "value1", "value2" }); + } + } +} diff --git a/RESTfulServicesDemo/Controllers/WeatherForecastController.cs b/RESTfulServicesDemo/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..08481d8 --- /dev/null +++ b/RESTfulServicesDemo/Controllers/WeatherForecastController.cs @@ -0,0 +1,63 @@ +using Microsoft.AspNetCore.Mvc; + +namespace RESTfulServicesDemo.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(Name = "GetWeatherForecast")] + public IEnumerable Get() + { + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateTime.Now.AddDays(index), + TemperatureC = Random.Shared.Next(-20, 55), + Summary = Summaries[Random.Shared.Next(Summaries.Length)] + }) + .ToArray(); + } + + [HttpPost] + public IActionResult Post(WeatherForecast weatherForecast) + { + return Ok(); + } + + [HttpPost("Body")] + public IActionResult PostBody([FromBody]WeatherForecast weatherForecast) + { + return Ok(weatherForecast); + } + + [HttpPost("Form")] + public IActionResult PostForm([FromForm]WeatherForecast weatherForecast) + { + return Ok(weatherForecast); + } + + [HttpPost("Route/{id}")] + public IActionResult PostRoute([FromRoute] string id) + { + return Ok(id); + } + + [HttpPost("Query")] + public IActionResult PostQuery([FromQuery(Name ="Summary")] string summary) + { + return Ok(summary); + } + } +} \ No newline at end of file diff --git a/RESTfulServicesDemo/Program.cs b/RESTfulServicesDemo/Program.cs new file mode 100644 index 0000000..baa57cd --- /dev/null +++ b/RESTfulServicesDemo/Program.cs @@ -0,0 +1,46 @@ +namespace RESTfulServicesDemo +{ + public class Program + { + public static void Main(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + + // Add services to the container. + + builder.Services.AddControllers() + .ConfigureApiBehaviorOptions(options => + { + //options.SuppressModelStateInvalidFilter = true; + + ////Suppress all binding refernence + //options.SuppressInferBindingSourcesForParameters = true; + + ////Suppress multipart/form-data content type reference + //options.SuppressConsumesConstraintForFormFileParameters = true; + }) + ; + // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle + builder.Services.AddEndpointsApiExplorer(); + builder.Services.AddSwaggerGen(); + + var app = builder.Build(); + + // Configure the HTTP request pipeline. + if (app.Environment.IsDevelopment()) + { + app.UseSwagger(); + app.UseSwaggerUI(); + } + + app.UseHttpsRedirection(); + + app.UseAuthorization(); + + + app.MapControllers(); + + app.Run(); + } + } +} \ No newline at end of file diff --git a/RESTfulServicesDemo/Properties/launchSettings.json b/RESTfulServicesDemo/Properties/launchSettings.json new file mode 100644 index 0000000..c225a49 --- /dev/null +++ b/RESTfulServicesDemo/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:31953", + "sslPort": 44370 + } + }, + "profiles": { + "RESTfulServicesDemo": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7094;http://localhost:5159", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/RESTfulServicesDemo/RESTfulServicesDemo.csproj b/RESTfulServicesDemo/RESTfulServicesDemo.csproj new file mode 100644 index 0000000..2c33b1c --- /dev/null +++ b/RESTfulServicesDemo/RESTfulServicesDemo.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + + diff --git a/RESTfulServicesDemo/WeatherForecast.cs b/RESTfulServicesDemo/WeatherForecast.cs new file mode 100644 index 0000000..6d07306 --- /dev/null +++ b/RESTfulServicesDemo/WeatherForecast.cs @@ -0,0 +1,13 @@ +namespace RESTfulServicesDemo +{ + 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; } + } +} \ No newline at end of file diff --git a/RESTfulServicesDemo/appsettings.Development.json b/RESTfulServicesDemo/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/RESTfulServicesDemo/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/RESTfulServicesDemo/appsettings.json b/RESTfulServicesDemo/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/RESTfulServicesDemo/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}