.NET MVC 4 WebAPI POST not working

The main problem: GET is working, POST is not. I am new to WebAPI, so I'm probably doing something stupid, but I was joking a lot on the net trying to figure out why this is not working.

Pretty simple WebAPI C # application. I tried to bring this to very simple routes:

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        // Add a route where action is supplied, i.e. api/csv/LoadToSQL
        config.Routes.MapHttpRoute(
            name: "ActionApi",
            routeTemplate: "api/{controller}/{action}"
        );

Controller Class:

public class CsvController : ApiController
{
    // GET api/csv
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/csv/name
    public string Get(string csvName)
    {
        return "value";
    }

    // POST - api/csv/LoadToSQL
    // Load a CSV to SQL
    [HttpPost]
    [ActionName("LoadToSQL")]
    public HttpResponseMessage LoadToSQL(
        string storageAccount,
        string accessKey,
        string containerName,
        string csvFileName,
        string connectionString,
        string targetDatabaseName,
        string targetTableName)
    {
        try
        {
            // Retrieve storage account from connection string.
            CloudStorageAccount blobStorage = CloudStorageAccount.Parse(storageAccount);

Run it locally and plug in Fiddler. Try GET; work:

Fiddler get output

Try POST - crash:

Fiddler post output

My initial implementation only had

    // POST - api/csv
    // Load a CSV to SQL
    public HttpResponseMessage Post(

but it didn’t work, so I started trying to add certain routes and verb decorations.

It just does not see the POST action against this controller no matter what I do.

Any thoughts?

UPDATE - for each answer below, the correct way to do this work:

        // Add a route where action is supplied, i.e. api/csv/LoadToSQL
        config.Routes.MapHttpRoute(
            name: "ActionApi",
            routeTemplate: "api/{controller}/{action}/{storageAccount}/{accessKey}/{containerName}/{csvFileName}/{connectionString}/{targetDatabaseName}/{targetTableName}"
        );

All POST request parameters are displayed here.

+4
1

,

1-

public class LoadToSqlData
{
   public string storageAccount {get; set;}
   public string accessKey {get; set;}
   public string containerName {get; set;}
   public string csvFileName {get; set;}
   public string connectionString {get; set;}
   public string targetDatabaseName {get; set;}
   public string targetTableName {get; set;}
}

2- :

[HttpPost]
[ActionName("ActionApi")]
[Route("api/Csv/LoadToSQL")]
public HttpResponseMessage LoadToSQL([FromBody]LoadToSqlData data)

[Route],[ActionName] [FromBody].

, .

+1

All Articles