Skip to content

Commit 068820d

Browse files
committed
Minimal APIs with ASP.NET 6.0
0 parents  commit 068820d

File tree

550 files changed

+362624
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

550 files changed

+362624
-0
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
bin
2+
obj
3+
Debug
4+
Release
5+
*.user
6+
*.suo
7+
.vs

00. Bare Minimum/Program.cs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
var builder = WebApplication.CreateBuilder(args);
3+
var app = builder.Build();
4+
app.MapGet("/", ([FromQuery]string? name) => new {message = $"Hello {name ?? "world"}"});
5+
app.Run();

00. Bare Minimum/Site.csproj

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.Extensions.Logging;
8+
using Site.Models;
9+
10+
namespace Site.Controllers
11+
{
12+
public class HomeController : Controller
13+
{
14+
private readonly ILogger<HomeController> _logger;
15+
16+
public HomeController(ILogger<HomeController> logger)
17+
{
18+
_logger = logger;
19+
}
20+
21+
public IActionResult Index()
22+
{
23+
return View();
24+
}
25+
26+
public IActionResult Privacy()
27+
{
28+
return View();
29+
}
30+
31+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
32+
public IActionResult Error()
33+
{
34+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
35+
}
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Extensions.Logging;
7+
8+
namespace Site.Controllers
9+
{
10+
[ApiController]
11+
[Route("api/[controller]")]
12+
public class WeatherForecastController : ControllerBase
13+
{
14+
private static readonly string[] Summaries = new[]
15+
{
16+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
17+
};
18+
19+
private readonly ILogger<WeatherForecastController> _logger;
20+
21+
public WeatherForecastController(ILogger<WeatherForecastController> logger)
22+
{
23+
_logger = logger;
24+
}
25+
26+
[HttpGet]
27+
public IEnumerable<WeatherForecast> Get()
28+
{
29+
var rng = new Random();
30+
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
31+
{
32+
Date = DateTime.Now.AddDays(index),
33+
TemperatureC = rng.Next(-20, 55),
34+
Summary = Summaries[rng.Next(Summaries.Length)]
35+
})
36+
.ToArray();
37+
}
38+
}
39+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace Site.Models
4+
{
5+
public class ErrorViewModel
6+
{
7+
public string? RequestId { get; set; }
8+
9+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
10+
}
11+
}

01. ASP.NET 5.0/Pages/About.cshtml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@page
2+
@model AboutModel
3+
@{
4+
ViewData["Title"] = "About page";
5+
}
6+
7+
<div class="text-center">
8+
<h1 class="display-4">@ViewData["Title"]</h1>
9+
<p>This .NET 5.0 sample shows MVC, WebAPI, and Razor Pages.</p>
10+
</div>

01. ASP.NET 5.0/Pages/About.cshtml.cs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.AspNetCore.Mvc.RazorPages;
7+
using Microsoft.Extensions.Logging;
8+
9+
namespace Site.Pages
10+
{
11+
public class AboutModel : PageModel
12+
{
13+
private readonly ILogger<AboutModel> _logger;
14+
15+
public AboutModel(ILogger<AboutModel> logger)
16+
{
17+
_logger = logger;
18+
}
19+
20+
public void OnGet()
21+
{
22+
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>@ViewData["Title"] - Site</title>
7+
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
8+
<link rel="stylesheet" href="~/css/site.css" />
9+
</head>
10+
<body>
11+
<header>
12+
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
13+
<div class="container">
14+
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">Site</a>
15+
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
16+
aria-expanded="false" aria-label="Toggle navigation">
17+
<span class="navbar-toggler-icon"></span>
18+
</button>
19+
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
20+
<ul class="navbar-nav flex-grow-1">
21+
<li class="nav-item">
22+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
23+
</li>
24+
<li class="nav-item">
25+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
26+
</li>
27+
<li class="nav-item">
28+
<a class="nav-link text-dark" asp-area="" asp-page="/About">About</a>
29+
</li>
30+
<li class="nav-item">
31+
<a class="nav-link text-dark" href="/swagger/">Swagger</a>
32+
</li>
33+
<li class="nav-item">
34+
<a class="nav-link text-dark" href="/api/WeatherForecast">Weather Forecast</a>
35+
</li>
36+
</ul>
37+
</div>
38+
</div>
39+
</nav>
40+
</header>
41+
<div class="container">
42+
<main role="main" class="pb-3">
43+
@RenderBody()
44+
</main>
45+
</div>
46+
47+
<footer class="border-top footer text-muted">
48+
<div class="container">
49+
&copy; 2021 - Site - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
50+
</div>
51+
</footer>
52+
53+
<script src="~/lib/jquery/dist/jquery.min.js"></script>
54+
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
55+
<script src="~/js/site.js" asp-append-version="true"></script>
56+
57+
@await RenderSectionAsync("Scripts", required: false)
58+
</body>
59+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
2+
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@using Site
2+
@namespace Site.Pages
3+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@{
2+
Layout = "_Layout";
3+
}

01. ASP.NET 5.0/Program.cs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging;
9+
10+
namespace Site
11+
{
12+
public class Program
13+
{
14+
public static void Main(string[] args)
15+
{
16+
CreateHostBuilder(args).Build().Run();
17+
}
18+
19+
public static IHostBuilder CreateHostBuilder(string[] args) =>
20+
Host.CreateDefaultBuilder(args)
21+
.ConfigureWebHostDefaults(webBuilder =>
22+
{
23+
webBuilder.UseStartup<Startup>();
24+
});
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:39069",
7+
"sslPort": 44390
8+
}
9+
},
10+
"profiles": {
11+
"Site": {
12+
"commandName": "Project",
13+
"dotnetRunMessages": "true",
14+
"launchBrowser": true,
15+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
}
19+
},
20+
"IIS Express": {
21+
"commandName": "IISExpress",
22+
"launchBrowser": true,
23+
"environmentVariables": {
24+
"ASPNETCORE_ENVIRONMENT": "Development"
25+
}
26+
}
27+
}
28+
}

01. ASP.NET 5.0/Site.csproj

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
10+
</ItemGroup>
11+
12+
</Project>

01. ASP.NET 5.0/Startup.cs

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.AspNetCore.HttpsPolicy;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.Extensions.Configuration;
10+
using Microsoft.Extensions.DependencyInjection;
11+
using Microsoft.Extensions.Hosting;
12+
using Microsoft.Extensions.Logging;
13+
using Microsoft.OpenApi.Models;
14+
15+
namespace Site
16+
{
17+
public class Startup
18+
{
19+
public Startup(IConfiguration configuration)
20+
{
21+
Configuration = configuration;
22+
}
23+
24+
public IConfiguration Configuration { get; }
25+
26+
// This method gets called by the runtime. Use this method to add services to the container.
27+
public void ConfigureServices(IServiceCollection services)
28+
{
29+
services.AddControllersWithViews();
30+
services.AddRazorPages();
31+
services.AddSwaggerGen(c =>
32+
{
33+
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Site", Version = "v1" });
34+
});
35+
}
36+
37+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
38+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
39+
{
40+
if (env.IsDevelopment())
41+
{
42+
app.UseDeveloperExceptionPage();
43+
app.UseSwagger();
44+
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Site v1"));
45+
}
46+
else
47+
{
48+
app.UseExceptionHandler("/Home/Error");
49+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
50+
app.UseHsts();
51+
}
52+
53+
app.UseHttpsRedirection();
54+
app.UseStaticFiles();
55+
56+
app.UseRouting();
57+
58+
app.UseAuthorization();
59+
60+
app.UseEndpoints(endpoints =>
61+
{
62+
endpoints.MapControllerRoute(
63+
name: "default",
64+
pattern: "{controller=Home}/{action=Index}/{id?}"
65+
);
66+
endpoints.MapRazorPages();
67+
});
68+
}
69+
}
70+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@{
2+
ViewData["Title"] = "Home Page";
3+
}
4+
5+
<div class="text-center">
6+
<h1 class="display-4">@ViewData["Title"]</h1>
7+
<p>This .NET 5.0 sample shows MVC, WebAPI, and Razor Pages.</p>
8+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@{
2+
ViewData["Title"] = "Privacy Policy";
3+
}
4+
<h1>@ViewData["Title"]</h1>
5+
6+
<p>Use this page to detail your site's privacy policy.</p>

0 commit comments

Comments
 (0)