Last active
January 18, 2024 22:13
-
-
Save JerryNixon/e19110517e7f1aa3a9c41874f0c7b8a5 to your computer and use it in GitHub Desktop.
Call an ASP.NET Minimal API from a Blazor WebAssembly App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Change this to your project namespace | |
using Sample.Shared; | |
var CorsPolicyName = "MyCorsApiPolicy"; | |
var builder = WebApplication.CreateBuilder(args); | |
builder.Services.AddCors(options => | |
{ | |
options.AddPolicy(name: CorsPolicyName, policy => | |
{ | |
policy.AllowAnyMethod(); | |
policy.AllowAnyOrigin(); | |
policy.AllowAnyHeader(); | |
}); | |
}); | |
var app = builder.Build(); | |
app.UseCors(CorsPolicyName); | |
app.MapGet("/config/{id}", GetHandler); | |
app.MapPost("/config", PutHandler); | |
app.Run(); | |
IResult GetHandler(int id) | |
{ | |
// TODO: Get configuration from database | |
var configuration = new Configuration | |
{ | |
ConnectionString = "default value one", | |
DatabaseType = "default value two" | |
}; | |
System.Diagnostics.Debugger.Break(); | |
return Results.Ok(configuration); | |
} | |
IResult PutHandler(Configuration configuration) | |
{ | |
// TODO: Save configuration to database | |
System.Diagnostics.Debugger.Break(); | |
return Results.Ok(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@page "/" | |
@using Sample.Shared | |
@using Microsoft.Fast.Components.FluentUI | |
<h1 style="color:red;">@Message</h1> | |
<h4>Connection String</h4> | |
<FluentTextField @bind-Value=Model.ConnectionString></FluentTextField> | |
<h4>Database Type</h4> | |
<FluentTextField @bind-Value=Model.DatabaseType></FluentTextField> | |
<p> | |
<FluentButton @onclick="LoadAsync">Load</FluentButton> | |
<FluentButton @onclick="SaveAsync">Save</FluentButton> | |
</p> | |
@code { | |
[Inject] | |
public HttpClient Http { get; set; } = default!; | |
public readonly string Url = "https://localhost:7266/config"; | |
public Configuration Model { get; set; } = new(); | |
public string Message { get; set; } = string.Empty; | |
protected override async Task OnInitializedAsync() | |
{ | |
await LoadAsync(); | |
} | |
public async Task LoadAsync() | |
{ | |
try | |
{ | |
var model = await Http.GetFromJsonAsync<Configuration>(Url + "/123"); | |
Model = model ?? throw new NullReferenceException("Result is null"); | |
} | |
catch (Exception ex) | |
{ | |
Message = ex.Message; | |
System.Diagnostics.Debugger.Break(); | |
} | |
} | |
public async Task SaveAsync() | |
{ | |
try | |
{ | |
await Http.PostAsJsonAsync(Url, Model); | |
} | |
catch (Exception ex) | |
{ | |
Message = ex.Message; | |
System.Diagnostics.Debugger.Break(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NuGet: Microsoft.Fast.Components.FluentUI | |
// change this to your project namespace | |
using Sample.UI; | |
using Microsoft.AspNetCore.Components.Web; | |
using Microsoft.AspNetCore.Components.WebAssembly.Hosting; | |
using Microsoft.Fast.Components.FluentUI; | |
var builder = WebAssemblyHostBuilder.CreateDefault(args); | |
builder.RootComponents.Add<App>("#app"); | |
builder.RootComponents.Add<HeadOutlet>("head::after"); | |
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); | |
builder.Services.AddFluentUIComponents(); | |
await builder. Build().RunAsync(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is all you need. Three projects:
Blazor WebAssembly App Empty Project / .NET 7.0
ASP.NET Core Empty Project / .NET 8.0
Class Library Project / .NET 7.0