Skip to content

Commit a06ffd8

Browse files
Merge branch 'main' into seo/92160-dotnet-stylecop-analyzers-implementation
2 parents 3744d1c + 5b83306 commit a06ffd8

46 files changed

Lines changed: 1436 additions & 46 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.12" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Microsoft.AspNetCore.Cors;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace Api.Controllers;
5+
6+
[ApiController]
7+
[Route("[controller]")]
8+
public class WeatherForecastController : ControllerBase
9+
{
10+
private static readonly string[] Summaries =
11+
[
12+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
13+
];
14+
15+
[EnableCors("CorsPolicy")]
16+
[HttpGet]
17+
public IEnumerable<WeatherForecast> Get()
18+
{
19+
var rng = new Random();
20+
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
21+
(
22+
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
23+
rng.Next(-20, 55),
24+
Summaries[rng.Next(Summaries.Length)]
25+
))
26+
.ToArray();
27+
}
28+
29+
[EnableCors("AnotherCorsPolicy")]
30+
[HttpGet("anotherPolicyExample")]
31+
public IActionResult GetTempAboveLimit()
32+
{
33+
return Ok("This action is protected with another named CORS policy");
34+
}
35+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using Microsoft.Net.Http.Headers;
2+
3+
var builder = WebApplication.CreateBuilder(args);
4+
5+
const string policyName = "CorsPolicy";
6+
const string anotherPolicyName = "AnotherCorsPolicy";
7+
8+
builder.Services.AddCors(options =>
9+
{
10+
options.AddPolicy(name: policyName, builder =>
11+
{
12+
builder.WithOrigins("https://localhost:5011")
13+
.AllowAnyHeader()
14+
.WithMethods("GET");
15+
});
16+
options.AddPolicy(name: anotherPolicyName, builder =>
17+
{
18+
builder.WithOrigins("https://localhost:5021")
19+
.WithHeaders(HeaderNames.ContentType, HeaderNames.Accept)
20+
.WithMethods("GET")
21+
.WithExposedHeaders("X-Pagination");
22+
});
23+
});
24+
25+
builder.Services.AddControllers();
26+
27+
builder.Services.AddOpenApi();
28+
builder.Services.AddProblemDetails();
29+
30+
var app = builder.Build();
31+
32+
if (app.Environment.IsDevelopment())
33+
{
34+
app.MapOpenApi();
35+
app.UseDeveloperExceptionPage();
36+
}
37+
else
38+
{
39+
app.UseExceptionHandler();
40+
app.UseHsts();
41+
app.UseStatusCodePages();
42+
}
43+
44+
app.UseHttpsRedirection();
45+
46+
app.UseCors();
47+
48+
app.UseAuthentication();
49+
app.UseAuthorization();
50+
51+
app.MapControllers();
52+
53+
var summaries = new[]
54+
{
55+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
56+
};
57+
58+
app.MapGet("/minimal/weatherforecast", () =>
59+
{
60+
var forecast = Enumerable.Range(1, 5).Select(index =>
61+
new WeatherForecast
62+
(
63+
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
64+
Random.Shared.Next(-20, 55),
65+
summaries[Random.Shared.Next(summaries.Length)]
66+
))
67+
.ToArray();
68+
return forecast;
69+
})
70+
.WithName("GetWeatherForecast")
71+
.RequireCors(policyName);
72+
73+
app.Run();
74+
75+
public record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
76+
{
77+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
78+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"profiles": {
3+
"Api": {
4+
"commandName": "Project",
5+
"dotnetRunMessages": true,
6+
"launchBrowser": false,
7+
"launchUrl": "weatherforecast",
8+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development"
11+
}
12+
}
13+
}
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
},
9+
"AllowedHosts": "*"
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Router AppAssembly="@typeof(Program).Assembly">
2+
<Found Context="routeData">
3+
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
4+
</Found>
5+
<NotFound>
6+
<LayoutView Layout="@typeof(MainLayout)">
7+
<p>Sorry, there's nothing at this address.</p>
8+
</LayoutView>
9+
</NotFound>
10+
</Router>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="10.0.12" />
11+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="10.0.12" PrivateAssets="all" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
@page "/fetchdata"
2+
@inject HttpClient Http
3+
4+
<h1>Weather forecast</h1>
5+
6+
<p>This component demonstrates fetching data from the server.</p>
7+
8+
@if (forecasts == null)
9+
{
10+
<p><em>Loading...</em></p>
11+
}
12+
else
13+
{
14+
<table class="table">
15+
<thead>
16+
<tr>
17+
<th>Date</th>
18+
<th>Temp. (C)</th>
19+
<th>Temp. (F)</th>
20+
<th>Summary</th>
21+
</tr>
22+
</thead>
23+
<tbody>
24+
@foreach (var forecast in forecasts)
25+
{
26+
<tr>
27+
<td>@forecast.Date.ToShortDateString()</td>
28+
<td>@forecast.TemperatureC</td>
29+
<td>@forecast.TemperatureF</td>
30+
<td>@forecast.Summary</td>
31+
</tr>
32+
}
33+
</tbody>
34+
</table>
35+
}
36+
37+
@code {
38+
private WeatherForecast[]? forecasts;
39+
40+
protected override async Task OnInitializedAsync()
41+
{
42+
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("/weatherforecast");
43+
}
44+
45+
public class WeatherForecast
46+
{
47+
public DateOnly Date { get; set; }
48+
49+
public int TemperatureC { get; set; }
50+
51+
public string? Summary { get; set; }
52+
53+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
54+
}
55+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
2+
using Client;
3+
4+
var builder = WebAssemblyHostBuilder.CreateDefault(args);
5+
builder.RootComponents.Add<App>("#app");
6+
7+
builder.Services.AddScoped(_ => new HttpClient { BaseAddress = new Uri("https://localhost:5001") });
8+
9+
await builder.Build().RunAsync();

0 commit comments

Comments
 (0)