【问题标题】:How do I access a parameterized method inside a controller from a nuget package?如何从 nuget 包访问控制器内的参数化方法?
【发布时间】:2014-12-12 00:04:03
【问题描述】:

我有一个 nuget 包,其中包含一个 MVC 控制器 - MyController,它有一个方法 MethodWithStringParameter(string a) 和另一个方法 MethodWithoutParameters()
我已经在我的 MVC 服务中安装了这个 nuget 包并注册了控制器和路由。

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
        };
    }
}

当我尝试点击 MethodWithStringParameter 方法时,我得到一个异常

无效的 JSON 原语:a.

但是,我可以毫无问题地使用方法 MethodWithoutParameters

详细异常

类型:“System.ArgumentException”

消息:“无效的 JSON 原语:a。”

StackTrace:“在 System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() 在 System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 深度) 在 System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String输入,Int32 depthLimit,JavaScriptSerializer 序列化程序)在 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer 序列化程序,字符串输入,类型类型,Int32 depthLimit)在 System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext) 在系统。 Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext) 在 System.Web.Mvc.ControllerBase.get_ValueProvider() 在 System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) 在 System.Web.Mvc.ControllerActionInvoker.GetParameterValues (ControllerContext 控制器上下文,ActionDescript或 actionDescriptor) 在 System.Web.Mvc.Async.AsyncControllerActionInvoker.c__DisplayClass21.b__19(AsyncCallback asyncCallback, Object asyncState)"

【问题讨论】:

  • 我也尝试使用这里提供的一些解决方案 stackoverflow.com/questions/9846158/… 但这没有帮助。
  • 你要发送什么请求?
  • 使用 Chrome 的 Advanced Rest Client 应用程序将两者作为 post 请求发送。将 a="1" 作为第一种方法的原始主体传递,而第二种方法则不传递任何内容。 (Content-Type: application/json; charset=utf-8) 对于两者
  • 那不是有效的 JSON。你期待什么?

标签: c# .net json asp.net-mvc model-binding


【解决方案1】:

您是否尝试过将空正文发布到此网址http://domainname/My/MethodWithStringParameter?a=abcd

【讨论】:

    猜你喜欢
    • 2020-03-12
    • 2014-01-11
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    • 2011-10-22
    • 1970-01-01
    相关资源
    最近更新 更多