简体   繁体   English

MVC controller API 访问 Blazor 不与 Z303CB0EF9EDB9082D61BBBE5825D972 一起使用

[英]MVC controller API access in Blazor not working with .NET Core 3.0

I am trying to setup a Blazor Server side app, but running into an issue with the app reading data from my MVC Controller API.我正在尝试设置 Blazor 服务器端应用程序,但遇到应用程序从我的 MVC Controller API 读取数据的问题。 I have a controller for my model Study called StudyController.我有一个 controller 用于我的 model 研究,名为 StudyController。 I can access the json data for my GetAll() route "/studies" when I launch the Blazor app, but the Blazor app is not reading the data.当我启动 Blazor 应用程序时,我可以访问我的 GetAll() 路由“/studies”的 json 数据,但 Blazor 应用程序没有读取数据。 Code below:下面的代码:

StudyController:研究控制器:

[Route("studies")]
[ApiController]
public class StudyController : ControllerBase
{
    private StudyRepository _ourCustomerRespository;

    public StudyController()
    {
        _ourCustomerRespository = new StudyRepository();
    }

    //[Route("Studies")]
    [HttpGet]
    public List<Study> GetAll()
    {
        return _ourCustomerRespository.GetStudies();
    }

}

Razor page function section trying to access data: Razor 页面 function 部分试图访问数据:

@functions {

IList<Study> studies = new List<Study>();

protected async Task OnInitAsync()
{
    HttpClient Http = new HttpClient();
    studies = await Http.GetJsonAsync<List<Study>>("/studies");
}
}

Startup.cs configuration code: Startup.cs 配置代码:

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc(options => options.EnableEndpointRouting = false)
            .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

        services.AddControllers();

        services.AddRazorPages();
        services.AddServerSideBlazor();
        services.AddSingleton<WeatherForecastService>();


    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();

        app.UseStaticFiles();

        app.UseRouting();

        app.UseMvcWithDefaultRoute();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.MapBlazorHub();
            endpoints.MapFallbackToPage("/_Host");
        });
    }

It appears the issue was that OnInitAsync() no longer works in the latest version of Blazor.看来问题是OnInitAsync()不再适用于最新版本的 Blazor。 I switched to using OnInitializedAsync() and that data loaded correctly.我切换到使用OnInitializedAsync()并且正确加载了数据。

You Can get any exception like "An invalid request URI was provided. The request URI must either be an absolute URI or BaseAddress must be set."您可以得到任何异常,例如“提供了无效的请求 URI。请求 URI 必须是绝对 URI 或必须设置 BaseAddress。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何导航到 Blazor 应用程序中的 ASP.NET Core MVC 控制器? - How to navigate to an ASP.NET Core MVC controller in Blazor app? Blazor 服务器与 ASP.NET 核心 MVC controller,验证问题 - Blazor server with ASP.NET Core MVC controller, validation issue 将数据和文件从 MVC 控制器(NET Framework 4.6.1)发送到 WebAPI 控制器(NET Core 3.0) - Send data and file from MVC Controller (NET Framework 4.6.1) to WebAPI Controller (NET Core 3.0) ASP.NET Core 3.0 Web API 路由不起作用 - ASP.NET Core 3.0 Web API routing not working 如何在MVC .net核心中访问返回控制器的字段结果 - How to access field result from returned controller in MVC .net core 限制对dotnet Core MVC项目中的Web API 2控制器的访问 - Limit access to web api 2 controller in dotnet core mvc project CSS+JS 资源和 Blazor .NET Core 3.0 应用编译 - CSS+JS recources and Blazor .NET Core 3.0 application compilation 在ASP.NET Core MVC API控制器上单元测试AuthorizeAttribute - Unit testing an AuthorizeAttribute on an ASP.NET Core MVC API controller 如何在 api mvc 项目 net core 3.1 中向 controller 添加操作 - How to add action to controller in api mvc project net core 3.1 将表单发布到 ASP.NET Core MVC 中的控制器不起作用 - Post form to controller in ASP.NET Core MVC not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM