20

I have an asp.net core 2.1 project and I'm getting the following error in my controller action:

Cannot implicitly convert type 'Microsoft.AspNetCore.Mvc.BadRequestObjectResult' to 'System.Collections.Generic.IList'. An explicit conversion exists (are you missing a cast?)

This is my code:

[HttpPost("create")]
[ProducesResponseType(201, Type = typeof(Todo))]
[ProducesResponseType(400)]
public async Task<IList<Todo>> Create([FromBody]TodoCreateViewModel model)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);   // This is the line that causes the intellisense error
    }

    await _todoRepository.AddTodo(model);
    return await GetActiveTodosForUser();
}


[HttpGet("GetActiveTodosForUser")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<IList<Todo>> GetActiveTodosForUser(string UserId = "")
{
    if (string.IsNullOrEmpty(UserId))
    {
        UserId = HttpContext.User.FindFirstValue(ClaimTypes.Sid);
    }
    return await _todoRepository.GetAll(UserId, false);
}

What am I doing wrong?

1
  • Your function is written to return Task<IList<Todo>> and BadRequest(ModelState) obviously inst't of that type Commented Aug 13, 2018 at 7:15

3 Answers 3

29

Your action return type does not take in mind possible BadRequest.

Instead of direct usage of IList<Todo> you need to wrap it with generic ActionResult type.

public async Task<ActionResult<IList<Todo>>> Create(...

Here are the related docs.

Sign up to request clarification or add additional context in comments.

Comments

14

For ASP.NET Core 2.1, you should use ActionResult<T> but there is a limitation with Interface's.

This Works

public ActionResult<IList<string>> Create()
{  
    return new List<string> { "value1", "value2" };
}

Doesn't Work

public ActionResult<IList<string>> Create()
{
    //DOESN'T COMPILE:
    //Error CS0029  Cannot implicitly convert type
    //'System.Collections.Generic.IList<string>' 
    //to 'Microsoft.AspNetCore.Mvc.ActionResult<System.Collections.Generic.IList<string>>'
    //the cast here is for demo purposes.
    //the problem will usually arise from a dependency that returns
    //an interface.
    var result = new List<string> { "value1", "value2" }
                     as IList<string>;
    return result;
}

C# doesn't support implicit cast operators on interfaces. Consequently, conversion of the interface to a concrete type is necessary to use ActionResult.

Source: ActionResult type



Sidenote: you don't need [FromBody] as ASP.NET will do that automatically. More here.

Comments

9

Actually you need to return IActionResult instead of IList for below ASP.NET Core 2.1,

public async Task<IActionResult> Create([FromBody]TodoCreateViewModel model)

Then it will work.

And for ASP.NET Core 2.1 as suggested by @amankkg,

public async Task<ActionResult<IList<Todo>>> Create([FromBody]TodoCreateViewModel model)

7 Comments

There is a generic ActionResult in ASP.NET Core 2.1
I want to create the ToDo then return the list of all the ToDos for the user which is why I'm returning a List
can't use interface's w/ ActionResult. More here
@spottedmahn I'm using Interfaces with ActionResult, then how you can say that it doesn't work. ActionResult<IEnumerable<PartViewModel>> Get(), Working example is here: devniggaspk.azurewebsites.net/admin/api/course/1/Clips/…
Weird, it didn't work for me and the documentation says it doesn't work either.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.