Skip to content

Commit 9e33d7e

Browse files
author
cleilson pereira
committed
🔥 Server webapp dotnet add
1 parent f00df1e commit 9e33d7e

10 files changed

Lines changed: 215 additions & 1 deletion

Client/Pages/FetchData.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ else
3939

4040
protected override async Task OnInitializedAsync()
4141
{
42-
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("sample-data/weather.json");
42+
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("api/WeatherForecast");
4343
}
4444

4545
public class WeatherForecast
Lines changed: 39 additions & 0 deletions
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 Server.Controllers
9+
{
10+
[ApiController]
11+
[Route("[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+
}

Server/Program.cs

Lines changed: 26 additions & 0 deletions
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 Server
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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:56258",
8+
"sslPort": 44375
9+
}
10+
},
11+
"profiles": {
12+
"IIS Express": {
13+
"commandName": "IISExpress",
14+
"launchBrowser": true,
15+
"launchUrl": "swagger",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
}
19+
},
20+
"Server": {
21+
"commandName": "Project",
22+
"dotnetRunMessages": "true",
23+
"launchBrowser": true,
24+
"launchUrl": "swagger",
25+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
26+
"environmentVariables": {
27+
"ASPNETCORE_ENVIRONMENT": "Development"
28+
}
29+
}
30+
}
31+
}

Server/Server.csproj

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

Server/Startup.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 Server
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+
30+
services.AddControllers();
31+
services.AddSwaggerGen(c =>
32+
{
33+
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Server", 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", "Server v1"));
45+
}
46+
47+
app.UseHttpsRedirection();
48+
49+
app.UseRouting();
50+
51+
app.UseAuthorization();
52+
53+
app.UseEndpoints(endpoints =>
54+
{
55+
endpoints.MapControllers();
56+
});
57+
}
58+
}
59+
}

Server/WeatherForecast.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace Server
4+
{
5+
public class WeatherForecast
6+
{
7+
public DateTime Date { get; set; }
8+
9+
public int TemperatureC { get; set; }
10+
11+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
12+
13+
public string Summary { get; set; }
14+
}
15+
}
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+
}

Server/appsettings.json

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+
}

TyeClientServerSingleOrigin.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.26124.0
55
MinimumVisualStudioVersion = 15.0.26124.0
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{8DB638E2-79CF-4BAE-BDEF-5AAD54B78D67}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server", "Server\Server.csproj", "{2DC94608-F542-43DD-9072-9B3D2E0320CE}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -30,5 +32,17 @@ Global
3032
{8DB638E2-79CF-4BAE-BDEF-5AAD54B78D67}.Release|x64.Build.0 = Release|Any CPU
3133
{8DB638E2-79CF-4BAE-BDEF-5AAD54B78D67}.Release|x86.ActiveCfg = Release|Any CPU
3234
{8DB638E2-79CF-4BAE-BDEF-5AAD54B78D67}.Release|x86.Build.0 = Release|Any CPU
35+
{2DC94608-F542-43DD-9072-9B3D2E0320CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
36+
{2DC94608-F542-43DD-9072-9B3D2E0320CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
37+
{2DC94608-F542-43DD-9072-9B3D2E0320CE}.Debug|x64.ActiveCfg = Debug|Any CPU
38+
{2DC94608-F542-43DD-9072-9B3D2E0320CE}.Debug|x64.Build.0 = Debug|Any CPU
39+
{2DC94608-F542-43DD-9072-9B3D2E0320CE}.Debug|x86.ActiveCfg = Debug|Any CPU
40+
{2DC94608-F542-43DD-9072-9B3D2E0320CE}.Debug|x86.Build.0 = Debug|Any CPU
41+
{2DC94608-F542-43DD-9072-9B3D2E0320CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
42+
{2DC94608-F542-43DD-9072-9B3D2E0320CE}.Release|Any CPU.Build.0 = Release|Any CPU
43+
{2DC94608-F542-43DD-9072-9B3D2E0320CE}.Release|x64.ActiveCfg = Release|Any CPU
44+
{2DC94608-F542-43DD-9072-9B3D2E0320CE}.Release|x64.Build.0 = Release|Any CPU
45+
{2DC94608-F542-43DD-9072-9B3D2E0320CE}.Release|x86.ActiveCfg = Release|Any CPU
46+
{2DC94608-F542-43DD-9072-9B3D2E0320CE}.Release|x86.Build.0 = Release|Any CPU
3347
EndGlobalSection
3448
EndGlobal

0 commit comments

Comments
 (0)