Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MinimalAPI - Did you mean to register the "Body (Inferred)" parameter(s) as a Service or apply the [FromService] or [FromBody] attribute?

I created an asp.net core empty project and whenever I am trying to run my application it gives me the error shown below. I cannot even hit the end point as soon I hit play it gives the error.

System.InvalidOperationException HResult=0x80131509 Message=Body was inferred but the method does not allow inferred body parameters. Below is the list of parameters that we found:

Parameter           | Source                        
---------------------------------------------------------------------------------
ur                  | Service (Attribute)
userLogin           | Body (Inferred)


Did you mean to register the "Body (Inferred)" parameter(s) as a Service or apply the [FromService] or [FromBody] attribute?

No idea why I am getting this error. I then tried adding [FromService] and it says the same error as well. I read this article for the same issue but it says do not add [Bind] which I was not in the first place and to instead use [FromService] but I still get the same error. Is there something wrong I am doing?

Program.cs:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDbContext<ApplicationDbContext>(x =>
    x.UseSqlServer(builder.Configuration.GetConnectionString("Default")));

builder.Services.AddScoped<IUserRepository, UserRepository>();

builder.Services.AddEndpointsApiExplorer();

builder.Services.AddSwaggerGen();

var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.MapGet("/userLogin", (IUserRepository ur, UserLogin userLogin) =>
{
    return ur.Get(userLogin);
});

if (app.Environment.IsDevelopment())
{
    app.UseSwagger(x => x.SerializeAsV2 = true);
    app.UseSwaggerUI();
}

app.Run();

UserLogin:

 [Keyless]
public class UserLogin
{
    public string Username { get; set; }
    public string Password { get; set; }
}

UserRepository:

public User Get(UserLogin userLogin)
        {   // get the username and password make sure what was entered matches in the DB then return the user
            var username =_dbContext.Users.Find(userLogin.Username, StringComparison.OrdinalIgnoreCase);

            return username;
        }
like image 932
MarkCo Avatar asked Sep 01 '25 10:09

MarkCo


1 Answers

In my case I forgot to add the below to my program.cs file above my app.Run();

builder.Services.AddScoped<IUserRepository, UserRepository>();
like image 189
Zach Gonzalez Avatar answered Sep 04 '25 22:09

Zach Gonzalez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!