ASP.NET Core API using [ApiController], Parameter always null

Viewed 55

I'm using ASP.NET Core to create a REST API. If I use the [APIController] class attribute, the method that uses the POST method with complex parameters always gets null value even though using/without using [FromBody] (I'm using Postman, Raw Json). But if I omit the [APIController] attribute, the parameters on the POST method work normally. I'm using ASP.NET Core 6. What is the effect without using the [APIController] attribute?

Program.cs

var builder = WebApplication.CreateBuilder(args);

builder.Services.Configure<Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions>(options =>
{
    options.AllowSynchronousIO = true;
});

builder.Services.Configure<IISServerOptions>(options =>
{
    options.AllowSynchronousIO = true;
});

// Add services to the container.
builder.Services.AddMvc(option =>
    {
        option.AllowEmptyInputInBodyModelBinding = true;
        option.EnableEndpointRouting = true;
        option.FormatterMappings.SetMediaTypeMappingForFormat("json", Microsoft.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json"));
        
    }).AddNewtonsoftJson(opt => {
        opt.SerializerSettings.DateFormatString = "dd/MM/yyyy HH:mm:ss";
    }).AddJsonOptions(options => {
        options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
        options.JsonSerializerOptions.PropertyNamingPolicy = null;
    });

builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
builder.Services.AddEndpointsApiExplorer();

builder.Services.AddAuthentication();
builder.Services.AddAuthorization();

var app = builder.Build();

app.UseHttpsRedirection();
app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});

app.Run();

Model:

public class BillRequest
{
    public string? CompCode { get; set; } = String.Empty;
    public string? CustNo { get; set; } = String.Empty;
    public string? ID { get; set; } = String.Empty;
    public string? Type { get; set; } = String.Empty;
    public string? TransDate { get; set; } = String.Empty;
    public string? Remark { get; set; } = String.Empty;

    public BillRequest()
    {

    }
}

Controller:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;

namespace FI.Controllers;

[ApiController]
[Route("[Controller]")]
public class VAController : ControllerBase
{
    private readonly ILogger<VAController> _logger;

    public VAController(ILogger<VAController> logger)
    {
        _logger = logger;
    }
    
    [HttpPost]
    //[Authorize(AuthenticationSchemes = VAAuthSchemeConstants.VAAuthScheme)]
    [Route("[Action]")]
    public BillRequest Bills(BillRequest billReq)
    {
        try
        {
            if (ModelState.IsValid)
            {
                return new BillRequest
                {
                    CompanyCode = "Test - success"
                };
            }
            else
            {
                return new BillRequest()
                {
                    CompanyCode = "Test - not valid model"
                };
            }
        }
        catch(Exception ex)
        {
            return new BillRequest()
            {
                CompCode = "Test - error"
            };
        }
    }
}

Postman Payload (Postman Body (Raw, Json)):

{
    "CompCode": "Test Comp"
    "CustNo": "1235",
    "ID": "123123123",
    "Type": "01",
    "TransDate": "Test Date",
    "Remark": "My Remark"
}

Even though I changed the parameter using a string the parameter value is still null.

0 Answers
Related