.net 5 Web API controller action arguments are always null : I did see a few threads here addressing similar issue but unfortunately nothing could solve my problem (so far).

**CONTROLLER METHOD**

Following is my controller method:

<!-- begin snippet: js hide: false -->

<!-- language: lang-html -->

    namespace apiservice.Controllers{
      [Route("api/[controller]")]
      public class BookStoreController : Controller{
        private BookContext _ctx;
        public BookStoreController(BookContext context)
        {
          _ctx = context;    
        }

        [EnableCors("AllowAll")]
        [RouteAttribute("SearchBooks")]
        [HttpGet("searchbooks/{key}")]
        public async Task<object> SearchBooks(string key){
          using(var cmd = _ctx.Database.GetDbConnection().CreateCommand()){
            cmd.CommandText = "SearchBooks";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@Key", SqlDbType.NVarChar) {Value = key});
                    
            if(cmd.Connection.State == ConnectionState.Closed)
              cmd.Connection.Open();
                    
            var retObj = new List<dynamic>();
            using (var dataReader = await cmd.ExecuteReaderAsync()){
              while(await dataReader.ReadAsync()){
                //Namespace for ExpandoObject: System.dynamic
                var dataRow = new ExpandoObject() as IDictionary<string, object>;
                for (var iFiled = 0; iFiled < dataReader.FieldCount; iFiled++)
                  dataRow.Add(dataReader.GetName(iFiled), dataReader[iFiled]);
                  retObj.Add((ExpandoObject)dataRow);
                }
              }
                    
              if(!retObj.Any())
                return JsonConvert.SerializeObject("No matching record found");
              else 
                return JsonConvert.SerializeObject(retObj);
              }
        }
      }
    }


<!-- end snippet -->

When I check the console for output it says

`fail: Microsoft.AspNet.Server.Kestrel[13]
An unhandled exception was thrown by the application. System.Data.SqlClient.SqlException (0x80131904): Procedure or function 'SearchBooks' expects parameter '@Key', which was not supplied.`


I have created another website locally, specially to test the CORS issue (which works fine). I am calling the above method via `AJAX` in the following way:

<!-- begin snippet: js hide: false -->

<!-- language: lang-html -->

    <script type='text/javascript'>
    $.ajax({
      type: "POST",
      url: "http://localhost:5000/api/bookstore/SearchBooks",
      data: { 'key': 'van' },
      dataType: 'json',
      contentType:"application/json",
      success: function (res) {
        $("#response").html(res);
      },
      error: function (err) {

      }
    });
    </script>  

<!-- end snippet -->

Problem is the value of argument `key` in controller method `SearchBooks` is always `null`!

But if I create a `model` (below)

**MODEL**

<!-- begin snippet: js hide: false -->

<!-- language: lang-html -->

    public class SearchViewModel{
      public string SearchKey {get; set;}
    }

<!-- end snippet -->

and then if I modify my `AJAX` to pass value to this `model` like following, everything works just fine!

<!-- begin snippet: js hide: false -->

<!-- language: lang-html -->

    <script type='text/javascript'>
      var searchModel={
        key: 'van'
      }
      
      $.ajax({
        type: "POST",
        data: JSON.stringify(searhModel),
        url: "http://localhost:5000/api/bookstore/searchbooks",
        contentType:"application/json",
        success: function (res) {
          $("#response").html(res);
        },
        error: function (err) {

        }
    });
    </script>

<!-- end snippet -->

Please help!