I'm trying to send a POST request using Fiddler as so:
POST http://localhost:50196/api/values/json HTTP/1.1
User-Agent: Fiddler
Content-Type: application/json;
Host: localhost:50196
Content-Length: 73
{"ssn":1605789787,"creditScore":598,"loanAmount":10.0,"loanDuration":360}
This is my WebAPI controller, where I deserialize it to a .NET type which is my POCO. Then I get the ssn and generate a rate which I then want to return.
public class ValuesController : ApiController
{
[ActionName("json")]
public string Post(string request)
{
var json = JsonConvert.DeserializeObject<JsonModel>(request);
var ss = json.Ssn;
var random = new Random();
var interestRate = random.Next(100, 500);
var rate = interestRate / 100;
var delta = "{\"ssn\":" + ss + ",\"interestRate\":" + rate + "}";
return delta;
}
}
POCO:
public class JsonModel
{
[JsonProperty("ssn")]
public string Ssn { get; set; }
[JsonProperty("creditScore")]
public int CreditScore { get; set; }
[JsonProperty("loanAmount")]
public decimal LoanAmount { get; set; }
[JsonProperty("loanDuration")]
public int LoanDuration { get; set; }
}
WebAPI config:
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}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: new {action = @"[a-zA-Z]+", id = @"\*"}
);
}
This is the error I'm getting:
[HttpException]: The controller for path '/api/values/json' was not found or does not implement IController. ved System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) ved System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) ved System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) ved System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) ved System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) ved
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) ved
System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() ved System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)