Skip to content

Commit

Permalink
[WebApp] Enable ImplictUsings and use file-scoped namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
iamcarbon committed Jan 19, 2023
1 parent 7ef2959 commit 4660426
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 101 deletions.
75 changes: 37 additions & 38 deletions src/Markdig.WebApp/ApiController.cs
Original file line number Diff line number Diff line change
@@ -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;
}
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)
// GET api/to_html?text=xxx&extension=advanced
[Route("api/to_html")]
[HttpGet()]
public object Get([FromQuery] string text, [FromQuery] string extension)
{
try
{
try
if (text == null)
{
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};
text = string.Empty;
}
catch (Exception ex)

if (text.Length > 1000)
{
return new { name = "markdig", html = "exception: " + GetPrettyMessageFromException(ex), version = Markdown.Version };
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)
private static string GetPrettyMessageFromException(Exception exception)
{
var builder = new StringBuilder();
while (exception != null)
{
var builder = new StringBuilder();
while (exception != null)
{
builder.Append(exception.Message);
exception = exception.InnerException;
}
return builder.ToString();
builder.Append(exception.Message);
exception = exception.InnerException;
}
return builder.ToString();
}
}
1 change: 1 addition & 0 deletions src/Markdig.WebApp/Markdig.WebApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<PreserveCompilationContext>true</PreserveCompilationContext>
<AssemblyName>Markdig.WebApp</AssemblyName>
<ImplicitUsings>enable</ImplicitUsings>
<OutputType>Exe</OutputType>
<PackageId>Markdig.WebApp</PackageId>
<ApplicationInsightsResourceId>/subscriptions/b6745039-70e7-4641-994b-5457cb220e2a/resourcegroups/Default-ApplicationInsights-EastUS/providers/microsoft.insights/components/Markdig.WebApp</ApplicationInsightsResourceId>
Expand Down
28 changes: 11 additions & 17 deletions src/Markdig.WebApp/Program.cs
Original file line number Diff line number Diff line change
@@ -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<Startup>();
});
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
84 changes: 38 additions & 46 deletions src/Markdig.WebApp/Startup.cs
Original file line number Diff line number Diff line change
@@ -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();
}

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 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)
// 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())
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseDeveloperExceptionPage();
}

app.UseHttpsRedirection();
app.UseHttpsRedirection();

app.UseRouting();
app.UseRouting();

app.UseAuthorization();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}

0 comments on commit 4660426

Please sign in to comment.