簡體   English   中英

我如何從C#(MVC)中的發布網址獲取NameValuePair數據

[英]How can i get NameValuePair data from post url in c#(MVC)

我通過添加NameValuePair從android設備發送HttpPost請求,但未獲取如何在Controller中獲取NameValuePair值。

這是發送請求的代碼:-

HttpPost request=new HttpPost(PostUrl);
HttpResponse response;
List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>(7);
    nameValuePairs.add(new BasicNameValuePair("userName",userName));
    nameValuePairs.add(new BasicNameValuePair("password",Password));
    nameValuePairs.add(new BasicNameValuePair("deviceId",deviceId));
    nameValuePairs.add(new BasicNameValuePair("subdomain",Subdomain));
    nameValuePairs.add(new BasicNameValuePair("deviceType",DeviceType));
    nameValuePairs.add(new BasicNameValuePair("uniqueId",UniqueId));
    nameValuePairs.add(new BasicNameValuePair("appVersion",AppVersion));
    request.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
    response = client.execute(request);

這是我發送的NameValuePair ..

現在,這是我發布這些數據的帳戶控制器和方法登錄名的代碼(C#)。

public LoginResponse login( NameValueCollection post, String UserName, String password, String deviceId, String subdomain, String deviceType,String uniqueId,String appVersion)
{
String User=Convert.toString(post["userName"]);


}

請告訴我如何使用此方法獲取NameValuePair。如果我通過附加到Post Url發送所有參數,則其工作正常,但是如果我通過NameValuePair發送,則User的值始終為null

嘗試這個:

public LoginResponse login(NameValueCollection post)
{
    string test1 = post.Get("userName");
    string test2 = post.Get("password");
}

另外,如果這是MVC,請選擇一個標准結果(在這種情況下,ContentResult返回一個字符串):

public ContentResult login(NameValueCollection post)
{
    LoginResponse loginResponse = //some method call...

    return Content("login", loginResponse)
}

讓我知道它是否有效。 如果沒有,我還有其他建議給您。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM