C# 服务器端blazor应用程序httpclient调用未到达我的web API控制器类

C# 服务器端blazor应用程序httpclient调用未到达我的web API控制器类,c#,asp.net-core-webapi,blazor,blazor-server-side,C#,Asp.net Core Webapi,Blazor,Blazor Server Side,我正在尝试使用.NETCore3.0预览版中的Blazor服务器端应用程序(RazorComponents)创建一个多人游戏。我的服务器项目中有一个SQLLite数据库和数据访问层来存储游戏信息。我有一个控制器类,也在服务器项目中,使用数据访问层从数据库返回对象。应用程序项目中我的cshtml页面正在进行api调用(使用注入的HttpClient),试图路由到我的控制器,但它没有到达服务器 我一直在尝试不同的Http客户端调用,但似乎都没有到达控制器。我觉得我遗漏了一些明显的东西,或者配置不正确

我正在尝试使用.NETCore3.0预览版中的Blazor服务器端应用程序(RazorComponents)创建一个多人游戏。我的服务器项目中有一个SQLLite数据库和数据访问层来存储游戏信息。我有一个控制器类,也在服务器项目中,使用数据访问层从数据库返回对象。应用程序项目中我的cshtml页面正在进行api调用(使用注入的HttpClient),试图路由到我的控制器,但它没有到达服务器

我一直在尝试不同的Http客户端调用,但似乎都没有到达控制器。我觉得我遗漏了一些明显的东西,或者配置不正确。我没有坚实的Razor/MVC背景,所以这对我来说有点难解决。为了进行测试,我尝试调用一个返回字符串的简单方法

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

private async Task SaveGame()
{
    var result = await Client.GetStringAsync("/api/Game/Test");
}
在Server.Controllers.GameController中:

public class GameController : Controller
{
    [HttpGet]
    [Route("api/Game/Test")]
    public string Test()
    {
        return "test";
    }
}
My Server.Startup文件注册MVC和Http服务,如下所示:

// 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?}"); });
    }
//此方法由运行时调用。使用此方法向容器中添加服务。
//有关如何配置应用程序的更多信息,请访问https://go.microsoft.com/fwlink/?LinkID=398940
public void配置服务(IServiceCollection服务)
{
服务。添加或组件
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware:信息:发送文件。请求路径:'/index.html'。物理路径:'{我的游戏路径}.Server\wwwroot\index.html'

所以,它似乎没有正确地路由到控制器。我一直在用谷歌搜索其他人是如何做到这一点的,似乎我做的是正确的。有人知道我遗漏了什么吗

我一直在学习以下教程:

试试这个:put
app.UseMvc(routes=>{routes.maprote(名称:“默认”,模板:{controller}/{action}/{id?}”);};
调用
app.UseStaticFiles();和app.UseRazorComponents();


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

尝试以下操作:put
app.UseMvc(routes=>{routes.maprote(name:“default”,模板:{controller}/{action}/{id}”);};
调用
app.UseStaticFiles();和app.UseRazorComponents();


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

以防有人遇到同样的问题,在我的情况下,这也起到了作用:

app.UseEndpoints(端点=>
{

endpoints.mapDefaultControllerOute();//以防有人遇到同样的问题,在我的例子中,这同样有效:

app.UseEndpoints(端点=>
{

endpoints.mapDefaultControllerOute()//成功了!谢谢!我从未意识到配置方法中的顺序是重要的。您是如何让HttpClient与Blazor服务器一起工作的?HttpClient是用于WebAssembly的客户端。成功了!谢谢!我从未意识到配置方法中的顺序是重要的。您是如何让HttpClient与Blazor服务器一起工作的?HttpClient是一个客户端WebAssembly的ide。