Код: Выделить всё
{
"BaseUrl": "https://localhost:7163"
}
< /code>
Это то, что мой файл программы.using System.Net.Http;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using ShoppingCart;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add("#app");
builder.RootComponents.Add("head::after");
string baseAddress =
builder.Configuration.GetValue("BaseUrl");
builder.Services.AddHttpClient("ShoppingCart", client => client.BaseAddress =
new Uri(baseAddress));
builder.Logging.SetMinimumLevel(LogLevel.Debug);
await builder.Build().RunAsync();
< /code>
И я пытаюсь прочитать строку Baseurl, как это: < /p>
string baseAddress =
builder.Configuration.GetValue("BaseUrl");
Код: Выделить всё
@code {
public IList
products;
public IList cart = new List();
private int total;
protected override async Task OnInitializedAsync()
{
Logger.LogInformation(".GetFromJsonAsync() method about to be called");
if (Http.BaseAddress == null)
{
Logger.LogWarning("Base address is null");
}
try
{
const string fullProductPath = "https://localhost:7163/sample-data/products.json";
string productFilePath = (Http.BaseAddress != null) ?
string.Concat(Http.BaseAddress.ToString(), "sample-data/products.json") : fullProductPath;
products = await Http.GetFromJsonAsync(productFilePath);
Logger.LogInformation(".GetFromJsonAsync() method was called on a previous line");
}
catch (Exception ex)
{
Logger.LogError(ex.Message);
}
}
private void AddProduct(Product product)
{
cart.Add(product);
total += product.Price;
}
private void DeleteProduct(Product product)
{
cart.Remove(product);
total -= product.Price;
}
}
< /code>
Единственная причина, по которой мой вызов getFromJsonAsync () даже работает, заключается в том, что я использую жесткую строку URI в случае, если базовый адрес не установлен должным образом. Теперь я прочитал в нескольких местах, которые программа. CS может быть не лучшим местом для установки базового адреса, поэтому мне интересно, должен ли я просто попытаться прочитать файл appsettings.json из моего компонента бритвы. Кроме того, я изменил переменную aspnetcore_environment
Код: Выделить всё
"profiles": {
"ShoppingCart": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "https://localhost:7163;http://localhost:5163",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Debug"
}
},
Подробнее здесь: https://stackoverflow.com/questions/764 ... file-and-n