Reputation: 41
I'm making a webapi app in C#. I'm using postman to send post requests to the api, but when Calling the function, the received parameter is null.
It seems that C# is not seeing any content of the post message.
This is my WebApiConfig.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace Lab1_v5
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
This is the Controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Lab1_API.Models;
namespace Lab1_API.Controllers
{
public class OperacionController : ApiController
{
public Operacion ProcesarResultado(Operacion op)
{
return Models.Operacion.Procesar(op);
}
}
}
This is the model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Lab1_API.Models
{
public class Operacion
{
private double value1, value2, resultado;
private int tipoOperacion;
public double Value1 { get => value1; set => value1 = value; }
public double Value2 { get => value2; set => value2 = value; }
public double Resultado { get => resultado; set => resultado = value; }
public int TipoOperacion { get => tipoOperacion; set => tipoOperacion = value; }
public Operacion(double pvalue1, double pvalue2, double presultado, int ptipoOperacion)
{
this.Value1 = pvalue1;
this.Value2 = pvalue2;
this.Resultado = presultado;
this.TipoOperacion = ptipoOperacion;
}
public Operacion(double pvalue1, double pvalue2, int ptipoOperacion)
{
this.Value1 = pvalue1;
this.Value2 = pvalue2;
this.Resultado = 0;
this.TipoOperacion = ptipoOperacion;
}
public static Operacion Procesar(Operacion op)
{
double result = 0;
switch (op.tipoOperacion)
{
case 1:
result = op.Value1 + op.Value2;
break;
case 2:
result = op.Value1 - op.Value2;
break;
case 3:
result = op.Value1 * op.Value2;
break;
case 4:
result = op.Value1 + op.Value2;
break;
}
op.resultado = result;
return op;
}
}
}
I'm sending this as Json with the header "application/json"
:
{
"Value1":1.0,
"Value2":1.0,
"Resultado":0.0,
"TipoOperacion":1
}
I've tryied pretty much everything and I can't seem to understand why I'm getting a null in the Controller.
EDIT -
The URL I'm using to reach the webapi is: http://localhost:55236/api/Operacion/ProcesarResultado.
Upvotes: 0
Views: 1371
Reputation: 4535
Aside from possibly not marking your method as accepting POST requests, your model is missing a parameterless constructor. It should be
public class Operacion
{
private double value1, value2, resultado;
private int tipoOperacion;
public double Value1 { get => value1; set => value1 = value; }
public double Value2 { get => value2; set => value2 = value; }
public double Resultado { get => resultado; set => resultado = value; }
public int TipoOperacion { get => tipoOperacion; set => tipoOperacion = value; }
public Operacion()
{
}
// other constructors and methods
}
The parameterless constructor is required for the framework to instantiate your class, as it can't use your constructors with parameters, because it doesn't know how to fill them.
If you have a model without explicitely declaring any constructors, a default parameterless constructor is automatically generated.
Upvotes: 0
Reputation: 1134
Problem is with Your constructor. Quick fix: Just add parameterless constructor:
public Operacion()
{
}
And don't forget to name Your action properly (start action name with: Post) or add [HttpPost] attribute before action definition (if You leave it like this it can add some more problems).
Upvotes: 0
Reputation: 282
You can try to mark your parameter with FromBodyAttribute
like so:
[HttpPost]
public Operacion ProcesarResultado([FromBody] Operacion op)
{
return Models.Operacion.Procesar(op);
}
This should suggest framework to deserialize request body. Without it it may be expecting form data.
Upvotes: 0
Reputation: 659
You need to add the HttpPost attribute or start the method name with Post.
[HttpPost]
public Operacion ProcesarResultado(Operacion op)
{
return Models.Operacion.Procesar(op);
}
or
public Operacion PostProcesarResultado(Operacion op)
{
return Models.Operacion.Procesar(op);
}
Upvotes: 2
Reputation: 2601
You need to specify that the method on the controller is for a POST request. Use the HttpPost
attribute.
[HttpPost]
public Operacion ProcesarResultado(Operacion op)
{
return Models.Operacion.Procesar(op);
}
Upvotes: 1
Reputation: 2085
You should specify the header of the request. Try to add
Content-Type: application/json
Upvotes: 1