【问题标题】:How to pass classic asp dictionary to .NET RESTful Web Api如何将经典的 asp 字典传递给 .NET RESTful Web Api
【发布时间】:2014-07-03 15:57:44
【问题描述】:

我在将数据参数从经典的 asp 应用程序传递到 .NET Web API 时遇到问题。似乎无论我做什么,我都无法访问 .NET Web API 中的参数。我在下面包含了我正在使用的代码:

经典的 ASP 代码:

Public Function GetResponse (HTTPMethod, ReqURL, QueryParamsOrArgs)

    Set XMLHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP")

    If HTTPMethod = "GET" Then
        ReqURL = ReqURL & "?" & QueryParamsOrArgs
        QueryParamsOrArgs = ""
    End If

    XMLHTTP.open HTTPMethod , ReqURL, false
    XMLHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    XMLHTTP.setRequestHeader "AuthToken", ServiceUrl_AuthToken
    XMLHTTP.send(QueryParamsOrArgs)

    If XMLHTTP.Status = 200 Then
        GetResponse = XMLHTTP.responseText
    Else
        GetResponse = ""
    End If

End Function

Public Function TestPOST(url)

    mydata = "value1=1&value2=2&value3=3"
    TestPOST = GetResponse("POST", url, mydata)

End Function

在调用 ASP 页面中:

dim oDataUtils  : Set oDataUtils = (New DataUtils)
myResponse = oDataUtils.TestPost(ServiceUrl_Base & "EventData/Post")

.NET Web API 操作方法:

[HttpPost]
    public HttpResponseMessage Post([FromBody]string value)
    {
        StringContent sc = new StringContent(value);
        sc.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        HttpResponseMessage resp = new HttpResponseMessage();
        resp.Content = sc;
        return resp;
    }

无论我发送什么,api方法内部的参数“value”的值始终为null。最终,我希望能够将整个字典或参数发送到此方法,但甚至无法获取最简单的组件(字符串)来传递。我在这里缺少什么?

我的最终目标是让这样的事情发挥作用:

[HttpPost]
    public HttpResponseMessage Post(Dictionary<string, object> data)
    {
        // Do something with the dictionary and then return a response...
        StringContent sc = new StringContent("test");
        sc.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        HttpResponseMessage resp = new HttpResponseMessage();
        resp.Content = sc;
        return resp;
    }

但我会在这一点上采取任何一种选择......

【问题讨论】:

    标签: c# asp.net dictionary asp-classic


    【解决方案1】:

    “问题”是:[FromBody]

    public HttpResponseMessage Post([FromBody]string value)
    

    用于“简单”类型,翻译的意思是,只有一个值。此外,API 期望使用FromBodyformat=value(请注意缺少“密钥”)。

    例如

    foo=bar //this will fail "null"
    
    =bar //this is good (no key)
    

    这将explain it in detail 即使主题是关于 jquery 的,它也会让您深入了解FromBody 的行为。

    使用FormDataCollection...

    第……

    【讨论】:

    • 谢谢你...我也找到了另一种解决方法,但是 FormDataCollection 允许消除一个步骤。 code string postdata = Request.Content.ReadAsStringAsync().Result; NameValueCollection NVC = HttpUtility.ParseQueryString(postdata); code
    【解决方案2】:

    如果你传递正确的 json 而不是 string ,它可能会起作用, 像你一样形成 json

    mydata="{'value':'value1=1&value2=2&value3=3'}"
    

    你也可以传递like的字典

    var data={};
    data.Value1="1";
    data.Value2="2";
    data.Value3="3";
    

    记住action方法中param的名字和json key的名字必须相同,这里是'values

    mydata={values:data};
    
    [HttpPost]
    public HttpResponseMessage Post(Dictionary<string, string> values)
    

    【讨论】:

    • 感谢您的回复,但是我正在从 VBScript(这不是通过 ajax 和 javascript 完成)向 .NET Web API 提出请求。我不能使用上面列出的字典符号。除非我遗漏了一些东西,否则我担心这些选项都不适合我使用。
    猜你喜欢
    • 1970-01-01
    • 2010-09-29
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    • 1970-01-01
    相关资源
    最近更新 更多