不从Blazor http客户端调用WEB API控制器类

问题描述

我正在尝试使用api从blazor应用程序中调用员工列表。当我尝试从我的服务控制器类中的API调用方法时,它将立即成为razor html而不是api控制器。这是我的代码

我的炽热项目startup.cs

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddRazorPages();
        services.AddServerSideBlazor();
        services.AddHttpClient<IEmployeeService,EmployeeService>(client =>
        {
            client.BaseAddress = new Uri("https://localhost:44300/");
        });
    }

EmployeeListBase.cs

 public class EmployeeListBase : ComponentBase
{
    [Inject]
    public IEmployeeService EmployeeService { get; set; }

    public IEnumerable<Employee> Employees { get; set; }

    protected override async Task OnInitializedAsync()
    {
        Employees = (await EmployeeService.GetEmployees()).ToList();
    }


}

员工服务班级

 public async  Task<IEnumerable<Employee>> GetEmployees()
    {
        
      
        return await httpClient.GetJsonAsync<Employee[]>("api/employees");
    }

但是当我调用httpClient.GetJsonAsync api时,它将立即转到EmployeeListBase剃刀html文件,而无需使用API​​控制器。 API地址运行正常。我复制了http api地址并将其粘贴在postman中,它工作正常。但是,当我通过blazor应用程序调用时,它不会进入api控制器。当我调试Employees时,数据将为null。任何帮助将不胜感激

@page "/"
@inherits EmployeeListBase

<h3>Employee List</h3>

<div class="card-deck">
    @foreach (var employee in Employees)
    {
        <div class="card m-3" style="min-width: 18rem; max-width:30.5%;">
            <div class="card-header">
                <h3>@employee.FirstName @employee.LastName</h3>
            </div>
            <img class="card-img-top imageThumbnail" src="@employee.PhotoPath" />
            <div class="card-footer text-center">
                <a href="#" class="btn btn-primary m-1">View</a>

                <a href="#" class="btn btn-primary m-1">Edit</a>

                <a href="#" class="btn btn-danger m-1">Delete</a>
            </div>
        </div>
    }
</div>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)