No 'MediaTypeFormatter' is available for reading an object of type 'String'

I am using ASP.NET web API. I have an action in my controller that works fine if there is no parameter. If there is a parameter, for example:

public string UploadFile(string actionType)

then my action is not called, and I get the following message viewed in Fiddler:

No "MediaTypeFormatter" is available for reading an object of type "String" with a media type of "multipart / form-data"

The route in mine is global.asxas follows:

"api/{controller}/{action}/{actionType}"

I use jQuery Post to call an action:

    function upload() {

        var actiontype = $("input:radio[name=actiontype]").val();
        var formData = new FormData($('form')[0]); 

        $.ajax({
            url: 'api/uploads/uploadfile/' + actiontype,
            type: 'POST',
            success: function (data) {
                $("#mydiv").append(data);
            },
            error: function (data) {
                $("#mydiv").append(data);
            },
            data: formData,
            cache: false,
            contentType: false, 
            processData: false
        });
    }; 

Here is my action method:

    public string UploadFile(string actionType)
    {
        if (Request.Content.IsMimeMultipartContent())
        {
            //Save file
            MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(HttpContext.Current.Server.MapPath("~/Files"));
            Request.Content.ReadAsMultipartAsync(provider);
        }

        return string.Format("Action {0} Complete!", actionType);
    }

Is this a known workaround? How can I perform a simple action with a parameter?

+3
source share
1 answer

, , , . , , , MVC ( , ). MVC , : /Controller/Action.

-API (/api/Controller), HTTP (post), (Post___) , .

, API Uploads, PostUploadFile.

   $.ajax({
        url: 'api/uploads/',
        type: 'POST',

, ...

  • "Post...", ,
  • url .
  • .

-, WebAPI. , "actionType" , .

. SO, .

+1

All Articles