Git Product home page Git Product logo

Comments (9)

abatishchev avatar abatishchev commented on June 26, 2024

Why are you trying to deserialize UpdateAccountCreditLimitRequest (single item) into FormDataCollection (array)? Why do you need MvcActionBinding here?

from webapicontrib.

BBauer42 avatar BBauer42 commented on June 26, 2024

Here is my SO post which should explain better.

I need MvcActionBinding here because I'm trying to populate a single incoming request object with a parameter FromUri and the rest FromBody. Web Api 2 doesn't allow you to do this out of the box and after searching, I thought this was the way to go.

from webapicontrib.

abatishchev avatar abatishchev commented on June 26, 2024

To formally match request model with model, try using ICollection<UpdateAccountCreditLimitRequest>.

But here's what works for me out of the box:

[Route("api/payment")]
public async Task<IHttpActionResult> PostStartPayment(StartPaymentRequest request, bool sync = true)
{
}

which is inboked by url:

POST http://example.com/api/payment/?sync=false

{
    "foo" : "bar" // request body goes here
}

In other words: try swap arguments order.

P.S.
You don't need [FromBody] since PUT is from body by default (by the HTTP spec).

P.P.S.
You don't need [HttpPut] if you name your method PutUpdateAccountCreditLimit().

from webapicontrib.

BBauer42 avatar BBauer42 commented on June 26, 2024

Changing the order of parameters doesn't help. Please see the stack overflow post. Following your example above, if there was a property called sync on your StartPaymentRequest object, it will NEVER be populated FromUrl. How can I get some of the request object's properties to populate via a combination of the body/url? Again I'm using Web Api (not MVC where this works out of the box).

This assumes the body is FormUrl encoded, however, my body is json.

from webapicontrib.

abatishchev avatar abatishchev commented on June 26, 2024

I don't think object's properties can be populated from both, only from either.

My example shows that different parameters can be populated differently (one form body, another from url).

from webapicontrib.

BBauer42 avatar BBauer42 commented on June 26, 2024

There are tests in this project showing that an object can be populated from both body and uri. Here is the controller in MvcActionValueBinderTests.cs

[MvcStyleBinding]
public class MvcController : ApiController
{
    [HttpGet]
    public Customer Combined(Customer item)
    {
        return item;
    }
}

Here is the test.

[Test]
public void TestOneFieldFromUriOneFromBody()
{
    var request = new HttpRequestMessage
    {
        Method = HttpMethod.Get,
        RequestUri = new Uri("http://localhost:8080/Mvc/Combined?age=10"),
        Content = FormUrlContent("name=Fred")
    };

    var response = client.SendAsync(request).Result;
    var actual = response.Content.ReadAsAsync<Customer>().Result;

    var expected = new Customer { name = "Fred", age = 10 };
    Assert.That(actual, Is.EqualTo(expected));
}

This works. However, there is one difference from this test and what I'm trying to do. I'd like to populate the object from ROUTE data and BODY data (not QUERYSTRING and BODY).

from webapicontrib.

abatishchev avatar abatishchev commented on June 26, 2024

I don't quite get what do you mean "from route". Does it work for you If do this in query string?

from webapicontrib.

allnnde avatar allnnde commented on June 26, 2024

Hi i have some problem in angular, in my client

    this.prueba = function (id, nombre) {

        return $http.post(ApiURL + "AEmpleado/prueba", { id: id, nombre: nombre })
            .then(
                function (respuesta) {

                    MensajeServices.MostarMensaje("", "Se elimino el Registro Correctamente", "success", null);
                    //vm.CargarTabla();
                    return respuesta
                },
                     function (respuesta) {
                         console.log(respuesta.data.Message);
                         return false;
                     }

);
}
in my server

[RoutePrefix("api/AEmpleado")]
[MvcStyleBinding]
public class AEmpleadoController : ApiController
{   

    [HttpPost, Route("prueba")]
    public IHttpActionResult prueba(int id, string nombre)
    {

        return Ok();

    }

}

anybody help?
Sorry for my english

from webapicontrib.

abatishchev avatar abatishchev commented on June 26, 2024

@allnnde please open a new issue. also explain the problem or error you're facing

from webapicontrib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.