1
votes

I have a nuget package which contains an MVC controller - MyController which has a method MethodWithStringParameter(string a) and another method MethodWithoutParameters().
I have installed this nuget package in my MVC service and have registered the controller and the routes.

public sealed class MyController : Controller
{
    [System.Web.Mvc.HttpPost]
    public HttpResponseMessage MethodWithStringParameter(string a)
    {
        return new HttpResponseMessage
        {
            StatusCode = HttpStatusCode.OK
        };
    }

    [System.Web.Mvc.HttpPost]
    public HttpResponseMessage MethodWithoutParameters()  
    {
        return new HttpResponseMessage
        {
            StatusCode = HttpStatusCode.OK
        };
    }
}

When I try hitting MethodWithStringParameter method, I get an exception

Invalid JSON primitive: a.

However, I am able to hit the method MethodWithoutParameters without any issues.

Detailed exception

Type: "System.ArgumentException"

Message: "Invalid JSON primitive: a."

StackTrace: " at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer) at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) at System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext) at System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext) at System.Web.Mvc.ControllerBase.get_ValueProvider() at System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) at System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.b__19(AsyncCallback asyncCallback, Object asyncState)"

1
I also tried using some of the solutions provided here stackoverflow.com/questions/9846158/… but that did not help. - KnightFox
What request are you sending? - SLaks
Sending both as post requests using Chrome's Advanced Rest Client app. Passing a="1" as raw body for the first method and nothing for the second. (Content-Type: application/json; charset=utf-8) for both - KnightFox
That isn't valid JSON. What do you expect? - SLaks

1 Answers

0
votes

Have you tried posting empty body to this url http://domainname/My/MethodWithStringParameter?a=abcd