在控制器中使用[FromBody]属性时,Blazor Server侧面应用程序(Razor组件)中的InputFormatters为空

问题描述 投票:1回答:1

我正在使用Asp.Net Core 3.0预览版中的Server side blazor(Razor Components)进行网页游戏。我有一个控制器类,我用来将游戏数据保存到服务器,但每当我使用有效的JSON主体发出请求时,控制器都无法格式化请求主体,因为它无法从上下文中找到任何IInputFormatter。

我已经能够在不使用[FromBody]属性的情况下执行简单的GET请求和POST,因此我知道我的控制器路由正在运行。但是无论何时我尝试使用HttpClient SendJsonAsync或PostJsonAsync方法并尝试使用[FromBody]属性读取JSON,我都会收到以下错误:

System.InvalidOperationException:'Microsoft.AspNetCore.Mvc.MvcOptions.InputFormatters'不能为空。从主体绑定至少需要一个'Microsoft.AspNetCore.Mvc.Formatters.IInputFormatter'。

我也直接安装了Microsoft.AspNetCore.Mvc.Formatters.Json到我的项目只是客栈,但没有运气。

我在我的Server.Startup类中注册并添加mvc到我的服务

// 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();

        //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.UseMvc(routes => { routes.MapRoute(name: "default", template: "{controller}/{action}"); });
        app.UseRazorComponents<App.Startup>();
    }

我的控制器类和方法:

public class GameController : Controller
{        
    [HttpPost]
    [Route("api/Game/SaveGame")]
    public string SaveGame([FromBody]GameInfoBody gameInfo)
    {
         //save the game to database
    }
}

我的客户在我的Game.cshtml页面中调用:

public async Task<string> SaveGameToDatabase(GameEngine game)
{
    var request = new GameInfoPostModel()
    {
        gameInfo = new GameInfoBody
        {
            ID = game.ID,
            GameEngine = game,
            Players = game.Teams.SelectMany(x => x.Players).Select(x => new PlayerGameMapping() { PlayerID = x.ID }).ToList()
        }
    };

    try
    {
        var result = await Client.SendJsonAsync<string>(HttpMethod.Post, "/api/Game/SaveGame", request);
        return result;
    }
    catch (Exception e)
    {
        return "Failed to save" + e.Message;
    }
}

完整堆栈跟踪:

System.InvalidOperationException: 'Microsoft.AspNetCore.Mvc.MvcOptions.InputFormatters' must not be empty. At least one 'Microsoft.AspNetCore.Mvc.Formatters.IInputFormatter' is required to bind from the body.
   at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BodyModelBinderProvider.GetBinder(ModelBinderProviderContext context)
   at Microsoft.AspNetCore.Mvc.ModelBinding.ModelBinderFactory.CreateBinderCoreUncached(DefaultModelBinderProviderContext providerContext, Object token)
   at Microsoft.AspNetCore.Mvc.ModelBinding.ModelBinderFactory.CreateBinder(ModelBinderFactoryContext context)
   at Microsoft.AspNetCore.Mvc.Controllers.ControllerBinderDelegateProvider.GetParameterBindingInfo(IModelBinderFactory modelBinderFactory, IModelMetadataProvider modelMetadataProvider, ControllerActionDescriptor actionDescriptor, MvcOptions mvcOptions)
   at Microsoft.AspNetCore.Mvc.Controllers.ControllerBinderDelegateProvider.CreateBinderDelegate(ParameterBinder parameterBinder, IModelBinderFactory modelBinderFactory, IModelMetadataProvider modelMetadataProvider, ControllerActionDescriptor actionDescriptor, MvcOptions mvcOptions)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvokerCache.GetCachedResult(ControllerContext controllerContext)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvokerProvider.OnProvidersExecuting(ActionInvokerProviderContext context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionInvokerFactory.CreateInvoker(ActionContext actionContext)
   at Microsoft.AspNetCore.Mvc.Routing.MvcEndpointDataSource.<>c__DisplayClass22_0.<CreateEndpoint>b__0(HttpContext context)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

阅读文档告诉我默认包含JsonFormatters。我已经使用Fiddler验证我的调用具有正确的内容类型和有效的JSON。我想如果它无法从上下文中找到任何InputFormatters,我必须没有正确配置服务,但是我没有找到其他人遇到这个问题,我不知道下一步该尝试什么。任何帮助将不胜感激,谢谢

asp.net-core-webapi blazor blazor-server-side
1个回答
0
投票

试试这个:(按照这个顺序...)

services.AddMvc().AddNewtonsoftJson();

services.AddRazorComponents<App.Startup>();
© www.soinside.com 2019 - 2024. All rights reserved.