简体   繁体   English

我如何从C#(MVC)中的发布网址获取NameValuePair数据

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

I am sending HttpPost request from android device by adding NameValuePair but not getting how to fetch the NameValuePair values in Controller. 我通过添加NameValuePair从android设备发送HttpPost请求,但未获取如何在Controller中获取NameValuePair值。

Here is the code for sending request:- 这是发送请求的代码:-

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

this is the NameValuePair i am sending .. 这是我发送的NameValuePair ..

Now Here is the code(C#) of my Account Controller and Method login where i am posting these data. 现在,这是我发布这些数据的帐户控制器和方法登录名的代码(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"]);


}

Please tell me how should i get the NameValuePair in this method .if i send all parameters by appending to Post Url then its working fine but if i am sending it through NameValuePair then the value of User is always null 请告诉我如何使用此方法获取NameValuePair。如果我通过附加到Post Url发送所有参数,则其工作正常,但是如果我通过NameValuePair发送,则User的值始终为null

Try this: 尝试这个:

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

Also, if this is MVC pick a standard result (in this case, ContentResult returns a string): 另外,如果这是MVC,请选择一个标准结果(在这种情况下,ContentResult返回一个字符串):

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

    return Content("login", loginResponse)
}

Let me know if it works. 让我知道它是否有效。 If not I have a couple of other suggestions for you. 如果没有,我还有其他建议给您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从发布网址C#获取数据 - Get the data from a post URL C# 我可以从一个 SQL 数据库中获取数据,然后使用 MVC C# 将该数据发布到不同服务器上的另一个 SQL 数据库中吗? - Can I get data from one SQL database and then post that data on another SQL database on a diferent server using MVC C# C#ASP.NET MVC-无法获取我从前端收到的POST数据的属性/数据 - C# ASP.NET MVC - Unable to get the properties/data of POST data I received from the frontend 如何从C#MVC中的记录获取数据 - How do i get data from records in C# MVC 如何从C#Xamarin中的HTTP发布中获取数据? - How can I get data from a HTTP post in C# Xamarin? 如何使用c#asp.net从URL获取数据? - How can I use c# asp.net to get data from url? C#如何从URL获取服务器错误? - C# How can I get server error from a URL? 如何使用asp.net mvc和razor模板引擎从c#中获取路由名称的URL? - How can I get the URL from route's name in c# using asp.net mvc and the razor template engine? 如何在MVC C#应用程序上将php作为帖子执行? - How can I execute php as an post on a MVC C# application? 我无法从 AJAX 中的 C# controller 发布请求中获取数据 - I can't get data from AJAX post request in C# controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM