服务器端blazor应用程序httpclient调用未达到我的Web API控制器类 [英] Server side blazor app httpclient calls are not reaching my web API controller class

查看:61
本文介绍了服务器端blazor应用程序httpclient调用未达到我的Web API控制器类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用.Net Core 3.0预览版中的Blazor服务器端应用程序(RazorComponents)创建多人游戏.我在Server项目中有一个SQL lite数据库和数据访问层,用于存储游戏信息.我在Server项目中也有一个Controller类,可以使用数据访问层从数据库中返回对象.我在App项目中的cshtml页面正在进行api调用(使用注入的HttpClient),试图将其路由到我的控制器,但未到达服务器.

I'm trying to create a multiplayer game using Blazor server-side app (RazorComponents) in .Net Core 3.0 preview. I have a SQL lite database and data access layer in my Server project to store the game information. I have a Controller class, also in the Server project, to return objects from the database using the data access layer. My cshtml page in the App project is making an api call (using injected HttpClient) trying to route to my controller, but it does not reach the server.

我一直在尝试不同的Http客户端调用,但是似乎没有一个到达控制器.我觉得我缺少明显的东西,或配置不正确的东西.我没有扎实的Razor/MVC背景,因此这对我来说很难解决.为了进行测试,我试图调用一个返回字符串的简单方法

I have been trying different Http client calls, but none seem to be reaching the controller. I feel like I'm missing something obvious, or configured something incorrectly. I don't have a solid Razor/MVC background, so this is a bit tough for me to troubleshoot. For testing, I'm trying to call a simple method that returns a string

在App.Game.cshtml @functions {}部分:

In App.Game.cshtml @functions { } section:

private async Task SaveGame()
{
    var result = await Client.GetStringAsync("/api/Game/Test");
}

在Server.Controllers.GameController中:

In Server.Controllers.GameController:

public class GameController : Controller
{
    [HttpGet]
    [Route("api/Game/Test")]
    public string Test()
    {
        return "test";
    }
}

我的Server.Startup文件注册MVC和Http服务,如下所示:

My Server.Startup file registers MVC and Http services like so:

// This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddRazorComponents<App.Startup>();
        services.AddMvc();

        //Code I got from internet to register httpclient service
        if (!services.Any(x => x.ServiceType == typeof(HttpClient)))
        {
            // Setup HttpClient for server side in a client side compatible fashion
            services.AddScoped<HttpClient>(s =>
            {
                // Creating the URI helper needs to wait until the JS Runtime is initialized, so defer it.
                var uriHelper = s.GetRequiredService<IUriHelper>();
                return new HttpClient
                {
                    BaseAddress = new Uri(uriHelper.GetBaseUri())
                };
            });
        }
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseStaticFiles();
        app.UseRazorComponents<App.Startup>();
        app.UseMvc(routes => { routes.MapRoute(name: "default", template: "{controller}/{action}/{id?}"); });
    }

当调用SaveGame()方法并执行了客户端调用时,我在输出中看到了这一点:

When the SaveGame() method is called and client call is executed, I see this in the output:

Microsoft.AspNetCore.Hosting.Internal.GenericWebHostService:信息:请求启动HTTP/1.1 GET http://localhost:59682/api/Game/Test
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware:信息:正在发送文件.请求路径:"/index.html".物理路径:"{我的游戏路径} .Server \ wwwroot \ index.html"

Microsoft.AspNetCore.Hosting.Internal.GenericWebHostService: Information: Request starting HTTP/1.1 GET http://localhost:59682/api/Game/Test
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware: Information: Sending file. Request path: '/index.html'. Physical path: '{path to my game}.Server\wwwroot\index.html'

因此,似乎无法正确路由到控制器.我一直在搜寻其他人是如何做到这一点的,似乎我正在以正确的方式来做.有人知道我想念什么吗?

So it seems like it isn't getting properly routed to the controller. I've been googling how other people have done this and it seems I'm doing it the right way. Anyone know what I'm missing?

我一直在关注像这样的教程: http://learn-blazor.com/architecture/rest-api/ https://medium.freecodecamp.org/如何使用blazor-and-entity-framework-core-1c1679d87c7e创建应用程序

I've been following tutorials like these: http://learn-blazor.com/architecture/rest-api/ https://medium.freecodecamp.org/how-to-create-an-application-using-blazor-and-entity-framework-core-1c1679d87c7e

推荐答案

尝试以下操作:输入 app.UseMvc(routes => {route.MapRoute(name:"default",模板:"{controller}/{action}/{id?});}); 在调用 app.UseStaticFiles();之前;和app.UseRazorComponents< App.Startup>();

Try this: put app.UseMvc(routes => { routes.MapRoute(name: "default", template: "{controller}/{action}/{id?}"); }); before calling app.UseStaticFiles(); and app.UseRazorComponents<App.Startup>();

在我看来,服务器正在发送index.html而不是(StaticFileMiddleware)

It seems to me that the server is sending index.html instead (StaticFileMiddleware)

这篇关于服务器端blazor应用程序httpclient调用未达到我的Web API控制器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆