English 中文(简体)
向 MVC4 api 服务器发送 T 与 RestSharp 的 SrestSharp 设置为无效天体
原标题:Posting T with RestSharp to MVC4 api server gives null object

我有一个普通的CUSer班

public class CUser
{
    public String Username { get; set; }
    public String Password { get; set; }
}

然后,我有这个密码 我的客户的客户 使用RestSharp

    public void CreateUser()
    {
        RestRequest request = new RestRequest(Method.POST);
        request.RequestFormat = DataFormat.Json;
        request.Resource = "user/{cUser}";

        CUser user = new CUser
        {
            Username = "Foo",
            Password = "BarBaz"
        };

        request.AddParameter("cUser", user, ParameterType.UrlSegment);

        client.PostAsync<int>(request, (response, handler) =>
            {
                System.Diagnostics.Debug.WriteLine(response.StatusDescription);
                System.Diagnostics.Debug.WriteLine("id: " + response.Data);
            });
    }

以及这个 http 路径在我的全球。 asax

            routes.MapHttpRoute(
            name: "CreateUser",
            routeTemplate: "api/{controller}/{cUser}",
            defaults: new
                          {
                              controller = "User",
                              action = "CreateUser",
                              cUser = RouteParameter.Optional
                          });

这个服务器代码可以处理它

    public int CreateUser(CUser cUser)
    {
        User user = new User(cUser);
        LoginManager manager = new LoginManager();
        return manager.CreateUser(user);
    }

但每次我运行CULSS的数值都是无效的(用户名和密码),

最佳回答

我认为这就是你想要的:

request.AddParameter("username", user.Username);
request.AddParameter("password", user.Password);

您重新做的事情将导致 cuser. tostring () 替换 占位符 。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签