has more than one parameter that was specified or inferred as bound from request body. Only one parameter per action may be bound from body

-2

Environment: I am using .NET verison 5.0.100-rc.2.20479.15, Microsoft Visual Studio Community 2019 Preview - Version 16.8.0 Preview 6.0 , Windows 10 64 version 2004, Microsoft SQL Server 2019, ASP.NET Core Blazor WebAssembly.

My dependency packages

<ItemGroup>
  <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.0-rc.2.20475.17" />
  <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.0-rc.2.20475.17" />
  <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="5.0.0-rc.2.20475.17" />
  <PackageReference Include="Microsoft.AspNetCore.ApiAuthorization.IdentityServer" Version="5.0.0-rc.2.20475.17" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0-rc.2.20475.6" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.0-rc.2.20475.6" />
</ItemGroup>

My controller

using a133.Server.Data;
using a133.Server.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace a133.Server.Controllers
{
    // Methods related to account(s): List all accounts, view detail an account. For example: Account 133 - Deducted Value Added Tax.
    [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class AccountDefaultController : ControllerBase
    {
        private readonly ApplicationDbContext db;

        IdentityUser identityUser = new IdentityUser();

        [HttpGet]
        public IEnumerable<AccountDefault> GetAll()
        {
            try
            {
                return db.AccountDefaults.ToList();
            }
            catch
            {
                throw;
            }
        }

        public Task<AccountDefault[]> Delete(AccountDefault item)
        {
            db.AccountDefaults.Remove(item);
            db.SaveChanges();
            IEnumerable<AccountDefault> ie = db.AccountDefaults.ToList();
            return Task.FromResult(ie.Cast<AccountDefault>().ToArray());
        }

        public Task<AccountDefault[]> GetAccountDefaultAsync()
        {
            IEnumerable<AccountDefault> ie = db.AccountDefaults.ToList();
            return Task.FromResult(ie.Cast<AccountDefault>().ToArray());
        }

        public async Task DeleteAsync(int key)
        {
            var item = await db.AccountDefaults.SingleOrDefaultAsync(x => x.Id == key);
            db.AccountDefaults.Remove(item);
            db.SaveChanges();
        }

        [NonAction]
        AccountDefault[] UpdateInternal(AccountDefault dataItem, Dictionary<string, object> newValue)
        {
            foreach (var field in newValue.Keys)
            {
                switch (field)
                {
                    case "RefTypeName":
                        dataItem.RefTypeName = (string)newValue[field];
                        break;
                    case "ColumnCaption":
                        dataItem.ColumnCaption = (string)newValue[field];
                        break;
                    case "Id":
                        dataItem.Id = (int)newValue[field];
                        break;
                    case "FilterCondition":
                        dataItem.FilterCondition = (string)newValue[field];
                        break;
                    case "DefaultValue":
                        dataItem.DefaultValue = (string)newValue[field];
                        break;
                    case "ColumnName":
                        dataItem.ColumnName = (string)newValue[field];
                        break;
                }
            }
            db.SaveChanges();
            IEnumerable<AccountDefault> ie = db.AccountDefaults.ToList();
            return ie.Cast<AccountDefault>().ToArray();
        }

        [HttpPut]
        public Task<AccountDefault[]> Update(AccountDefault dataItem, Dictionary<string, object> newValue)
        {
            return Task.FromResult(UpdateInternal(dataItem, newValue));
        }

        AccountDefault[] InsertInternal(Dictionary<string, object> newValue)
        {
            var dataItem = new AccountDefault();
            Update(dataItem, newValue);
            dataItem.CreatedDate = DateTime.Now;
            //System.Security.Claims.ClaimsPrincipal currentUser = this.User;
            //dataItem.CreatedBy = currentUser.Identity.Name;
            //dataItem.CreatedBy = identityUser.UserName;
            //dataItem.CreatedBy = User.Identity.Name;

            db.AccountDefaults.Add(dataItem);
            db.SaveChanges();
            IEnumerable<AccountDefault> ie = db.AccountDefaults.ToList();
            return ie.Cast<AccountDefault>().ToArray();
        }

        public Task<AccountDefault[]> Insert(Dictionary<string, object> newValue)
        {
            return Task.FromResult(InsertInternal(newValue));
        }

    }

}

My error

System.InvalidOperationException
  HResult=0x80131509
  Message=Action 'a133.Server.Controllers.AccountDefaultController.Update (a133.Server)' has more than one parameter that was specified or inferred as bound from request body. Only one parameter per action may be bound from body. Inspect the following parameters, and use 'FromQueryAttribute' to specify bound from query, 'FromRouteAttribute' to specify bound from route, and 'FromBodyAttribute' for parameters to be bound from body:
AccountDefault dataItem
Dictionary<string, object> newValue
  Source=Microsoft.AspNetCore.Mvc.Core
  StackTrace:
   at Microsoft.AspNetCore.Mvc.ApplicationModels.InferParameterBindingInfoConvention.InferParameterBindingSources(ActionModel action)
   at Microsoft.AspNetCore.Mvc.ApplicationModels.InferParameterBindingInfoConvention.Apply(ActionModel action)
   at Microsoft.AspNetCore.Mvc.ApplicationModels.ApiBehaviorApplicationModelProvider.OnProvidersExecuting(ApplicationModelProviderContext context)
   at Microsoft.AspNetCore.Mvc.ApplicationModels.ApplicationModelFactory.CreateApplicationModel(IEnumerable`1 controllerTypes)
   at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.GetDescriptors()
   at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.OnProvidersExecuting(ActionDescriptorProviderContext context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.UpdateCollection()
   at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.Initialize()
   at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.GetChangeToken()
   at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointDataSourceBase.<>c__DisplayClass11_0.<Subscribe>b__0()
   at Microsoft.Extensions.Primitives.ChangeToken.OnChange(Func`1 changeTokenProducer, Action changeTokenConsumer)
   at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointDataSourceBase.Subscribe()
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionEndpointDataSource..ctor(PageActionEndpointDataSourceIdProvider dataSourceIdProvider, IActionDescriptorCollectionProvider actions, ActionEndpointFactory endpointFactory, OrderedEndpointsSequenceProvider orderedEndpoints)
   at Microsoft.AspNetCore.Builder.RazorPagesEndpointRouteBuilderExtensions.GetOrCreateDataSource(IEndpointRouteBuilder endpoints)
   at Microsoft.AspNetCore.Builder.RazorPagesEndpointRouteBuilderExtensions.MapRazorPages(IEndpointRouteBuilder endpoints)
   at a133.Server.Startup.<>c.<Configure>b__5_0(IEndpointRouteBuilder endpoints) in C:\Users\donhuvy\source\repos\a133\Server\Startup.cs:line 73
   at Microsoft.AspNetCore.Builder.EndpointRoutingApplicationBuilderExtensions.UseEndpoints(IApplicationBuilder builder, Action`1 configure)
   at a133.Server.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env) in C:\Users\donhuvy\source\repos\a133\Server\Startup.cs:line 71
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
   at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.<Build>b__0(IApplicationBuilder builder)
   at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass15_0.<UseStartup>b__1(IApplicationBuilder app)
   at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
   at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
   at Microsoft.AspNetCore.Hosting.GenericWebHostService.<StartAsync>d__31.MoveNext()

enter image description here

How to fix it?

c#
asp.net-core
asp.net-core-webapi
blazor
blazor-webassembly
asked on Stack Overflow Nov 8, 2020 by Do Nhu Vy

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0