From 46604267194b2ce4e23c7cbc551714ec8db2de95 Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Wed, 18 Jan 2023 19:49:44 -0800 Subject: [PATCH] [WebApp] Enable ImplictUsings and use file-scoped namespaces --- src/Markdig.WebApp/ApiController.cs | 87 ++++++++++++------------ src/Markdig.WebApp/Markdig.WebApp.csproj | 1 + src/Markdig.WebApp/Program.cs | 28 +++----- src/Markdig.WebApp/Startup.cs | 86 +++++++++++------------ 4 files changed, 94 insertions(+), 108 deletions(-) diff --git a/src/Markdig.WebApp/ApiController.cs b/src/Markdig.WebApp/ApiController.cs index 4bbbb09c..2018c936 100644 --- a/src/Markdig.WebApp/ApiController.cs +++ b/src/Markdig.WebApp/ApiController.cs @@ -1,55 +1,54 @@ -using System; using System.Text; + using Microsoft.AspNetCore.Mvc; -namespace Markdig.WebApp +namespace Markdig.WebApp; + +public class ApiController : Controller { - public class ApiController : Controller + [HttpGet()] + [Route("")] + public string Empty() { - [HttpGet()] - [Route("")] - public string Empty() + return string.Empty; + } + + // GET api/to_html?text=xxx&extension=advanced + [Route("api/to_html")] + [HttpGet()] + public object Get([FromQuery] string text, [FromQuery] string extension) + { + try { - return string.Empty; + if (text == null) + { + text = string.Empty; + } + + if (text.Length > 1000) + { + text = text.Substring(0, 1000); + } + + var pipeline = new MarkdownPipelineBuilder().Configure(extension).Build(); + var result = Markdown.ToHtml(text, pipeline); + + return new {name = "markdig", html = result, version = Markdown.Version}; } - - // GET api/to_html?text=xxx&extension=advanced - [Route("api/to_html")] - [HttpGet()] - public object Get([FromQuery] string text, [FromQuery] string extension) + catch (Exception ex) { - try - { - if (text == null) - { - text = string.Empty; - } - - if (text.Length > 1000) - { - text = text.Substring(0, 1000); - } - - var pipeline = new MarkdownPipelineBuilder().Configure(extension).Build(); - var result = Markdown.ToHtml(text, pipeline); - - return new {name = "markdig", html = result, version = Markdown.Version}; - } - catch (Exception ex) - { - return new { name = "markdig", html = "exception: " + GetPrettyMessageFromException(ex), version = Markdown.Version }; - } - } - - private static string GetPrettyMessageFromException(Exception exception) - { - var builder = new StringBuilder(); - while (exception != null) - { - builder.Append(exception.Message); - exception = exception.InnerException; - } - return builder.ToString(); + return new { name = "markdig", html = "exception: " + GetPrettyMessageFromException(ex), version = Markdown.Version }; } } + + private static string GetPrettyMessageFromException(Exception exception) + { + var builder = new StringBuilder(); + while (exception != null) + { + builder.Append(exception.Message); + exception = exception.InnerException; + } + return builder.ToString(); + } } diff --git a/src/Markdig.WebApp/Markdig.WebApp.csproj b/src/Markdig.WebApp/Markdig.WebApp.csproj index 7ef583ee..d8cb2f0e 100644 --- a/src/Markdig.WebApp/Markdig.WebApp.csproj +++ b/src/Markdig.WebApp/Markdig.WebApp.csproj @@ -4,6 +4,7 @@ net6.0 true Markdig.WebApp + enable Exe Markdig.WebApp /subscriptions/b6745039-70e7-4641-994b-5457cb220e2a/resourcegroups/Default-ApplicationInsights-EastUS/providers/microsoft.insights/components/Markdig.WebApp diff --git a/src/Markdig.WebApp/Program.cs b/src/Markdig.WebApp/Program.cs index c17a2d1d..80487b84 100644 --- a/src/Markdig.WebApp/Program.cs +++ b/src/Markdig.WebApp/Program.cs @@ -1,22 +1,16 @@ -using System.IO; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Builder; -using Microsoft.Extensions.Hosting; +namespace Markdig.WebApp; -namespace Markdig.WebApp +public class Program { - public class Program + public static void Main(string[] args) { - public static void Main(string[] args) - { - CreateHostBuilder(args).Build().Run(); - } - - public static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - }); + CreateHostBuilder(args).Build().Run(); } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); } diff --git a/src/Markdig.WebApp/Startup.cs b/src/Markdig.WebApp/Startup.cs index 28701ec1..ce84a977 100644 --- a/src/Markdig.WebApp/Startup.cs +++ b/src/Markdig.WebApp/Startup.cs @@ -1,60 +1,52 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; +namespace Markdig.WebApp; -namespace Markdig.WebApp +public class Startup { - public class Startup + public Startup(IWebHostEnvironment env) { - public Startup(IWebHostEnvironment env) + var builder = new ConfigurationBuilder() + .SetBasePath(env.ContentRootPath) + .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); + + if (env.IsEnvironment("Development")) { - var builder = new ConfigurationBuilder() - .SetBasePath(env.ContentRootPath) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); - - if (env.IsEnvironment("Development")) - { - // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately. - builder.AddApplicationInsightsSettings(developerMode: true); - } - - builder.AddEnvironmentVariables(); - Configuration = builder.Build(); + // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately. + builder.AddApplicationInsightsSettings(developerMode: true); } - public IConfigurationRoot Configuration { get; } + builder.AddEnvironmentVariables(); + Configuration = builder.Build(); + } - // This method gets called by the runtime. Use this method to add services to the container - public void ConfigureServices(IServiceCollection services) + public IConfigurationRoot 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(); + + // Add framework services. + services.AddApplicationInsightsTelemetry(Configuration); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) + { + if (env.IsDevelopment()) { - services.AddControllers(); - - // Add framework services. - services.AddApplicationInsightsTelemetry(Configuration); + app.UseDeveloperExceptionPage(); } - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline - public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) + app.UseHttpsRedirection(); + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => { - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - - app.UseHttpsRedirection(); - - app.UseRouting(); - - app.UseAuthorization(); - - app.UseEndpoints(endpoints => - { - endpoints.MapControllers(); - }); - } + endpoints.MapControllers(); + }); } }