locked
Does the aspnetcore web API remove features such as [FromBody]? RRS feed

  • Question

  • User1849955458 posted
    The Android request parameter is in [fromform]
     
    Web request parameters in Query Params
     
    Is there any compatibility on the server side?
    Friday, October 11, 2019 3:50 AM

All replies

  • User1120430333 posted

    IMO, you can pass objects like a DTO or a POCO to/from the WebAPI.in Json object format.

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

    <copied>

    To force Web API to read a simple type from the request body, add the [FromBody] attribute to the parameter:

    <end>

    Friday, October 11, 2019 8:04 AM
  • User711641945 posted

    Hi laolaolulu,

    For your thread's title,asp.net core web api does not remoce the feature such as [FromBody].

    I am not clear what's the relationship between your title and the content of your thread.Do you mean that you want a attribute added in api action and it could receive both FromForm and FromQuery data?

    Best Regards,

    Rena

    Monday, October 14, 2019 8:33 AM
  • User1849955458 posted
    I'm sorry that my translation tool is not ideal.
     
     
     
    The request header of Android client is different from that of web client. In Web API, I can't receive the parameters of web client with [frombody], and I can't receive the parameters of Android client with [fromfrom].
    Wednesday, October 23, 2019 12:48 AM
  • User711641945 posted

    Hi laolaolulu,

    Could you share a simple demo that could reproduce your issue?

    For [FromForm],it is binding from form data,the Content-Type should be `application/x-www-form-urlencoded`.For [FromBody],it is binding from json,the Content-Type should be `application/json`.

    Best Regards,

    Rena

    Wednesday, October 23, 2019 5:16 AM
  • User1849955458 posted

    Android client:

    $.ajax({
    url:"api/getdata",
    data:{id:6},
    contentType:'application/x-www-form-urlencoded'
    success:function(result){ ... }
    });

    Web client:

    $.ajax({
    url:"api/getdata",
    data:{id:6},
    contentType:'application/json'
    success:function(result){ ... }
    });

    Api:

     public object GetData([FromBody] id)
    {
               ...
    }

    Client cannot modify。

    Wednesday, October 23, 2019 5:32 AM
  • User711641945 posted

    Hi laolaolulu,

    >In Web API, I can't receive the parameters of web client with [frombody], and I can't receive the parameters of Android client with [fromfrom].

    You means that you could not receive the data either from web client or from Android client?

    You pass a simple type parameter from your code,you just need to pass it on query string:

    1.View:

    var data = 6;
    console.log(id);
    $.ajax({
    url: "/api/getdata?id="+data,
    contentType: 'application/x-www-form-urlencoded',
    success:function(result){  }
    });

    2.Action:

    public object GetData(int id)
    {
        ...
    }

    Best Regards,

    Rena

    Wednesday, October 23, 2019 6:58 AM
  • User1849955458 posted

    I mean using the same webapi Action to process different contentType requests.

    Wednesday, October 23, 2019 7:01 AM
  • User-474980206 posted

    We would need to know which Ajax library you are using from android. If jquery, you need to make it a post.

    Wednesday, October 23, 2019 9:04 AM