Click here to Skip to main content
14,454,989 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi, I am trying to read stream data and write it to a table in core 2.2 web api and running into errors.

public class HomeController : Controller
    private readonly DbContext _context;
    private readonly IConfiguration Configuration;

    public HomeController(DbContext context, IConfiguration configuration)
        _context = context;
        //_roleManager = roleManager;
        Configuration = configuration;

    [Route("api/v1/test", Name = "Test")]
    public Task Post()
            using (var records = new StreamReader(Request.Body, Encoding.UTF8))
                return Ok("OK"); //Error: Cannot implicitly convert type 'Microsoft.AspNetCore.Mvc.OkObjectResult' to 'System.Threading.Tasks.Task'

        catch (Exception ex)
            return BadRequest(ex.Message); //Error: Cannot implicitly convert type 'Microsoft.AspNetCore.Mvc.BadRequestObjectResult' to 'System.Threading.Tasks.Task'

    public void ImportData(StreamReader records)
        string connectionstring = Configuration["ConnectionStrings:dbconnstr"];

        List test = new List();

        foreach (string record in records)
            Test t = new Test();
            string[] textpart = record.Split('|');
            t.ID = textpart[0];
            t.Name = textpart[1];
        var copyParameters = new[]
        using (var sqlCopy = new SqlBulkCopy(connectionstring))
            sqlCopy.DestinationTableName = "[Test]";
            sqlCopy.BatchSize = 500;
            using (var reader = ObjectReader.Create(test, copyParameters))

What I have tried:

I tried the above code which is a bit different from .net 4.6 framework and trying to make it work.
Updated 11-Dec-19 5:20am
Mehul M Thakkar 10-Dec-19 3:37am
try with "async Task"
Member 12586110 10-Dec-19 7:48am
Hi Mehul M Thakkar, Thanks very much for your response. Tried async Task and still get the same error.
ZurdoDev 10-Dec-19 13:51pm
How can we help you? You haven't told us what the errors are.
Member 12586110 10-Dec-19 14:43pm
Hi ZurdoDev Thank you for your response. I am trying to read stream of data using StreamReader class and write the content to a database table as part of the post method. This approach used to work fine in .net 4.6 framework. When I write this I get exceptions when I use return and not able to import into a table. Could you please help or guide me write this in .net core 2.2.
ZurdoDev 10-Dec-19 14:48pm
No, I can't help you because as I already said you haven't told us what the errors are.
Member 12586110 10-Dec-19 15:03pm
Hi ZurdoDev, With return Ok("OK"); Get the Error: Cannot implicitly convert type 'Microsoft.AspNetCore.Mvc.OkObjectResult' to 'System.Threading.Tasks.Task' and
return BadRequest(ex.Message); Get Error: Cannot implicitly convert type 'Microsoft.AspNetCore.Mvc.BadRequestObjectResult' to 'System.Threading.Tasks.Task'. Thank you.
ZurdoDev 10-Dec-19 15:06pm
This is now a different issue than what you had said before.

If you want to return a view that is not named the same as the method you are in, do return View(viewName);

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Edit- Just read your question again, even though I would use IFormFile for file transfers with a webapi but that is my personal prefernce

your error is in the signature of your method

public Task Post()

should be

public Task<IactionResult> Post()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100