【问题标题】:pass value as empty string to WEb API from typescript将值作为空字符串从打字稿传递给 WEB API
【发布时间】:2018-04-09 06:04:16
【问题描述】:

我想将一个空字符串从 javascript 传递给 C# web api。 但是当我从打字稿传递空字符串时,我在 webapi 参数中得到了 null 。 如何传递空字符串?

这是我的客户端 web api 调用:

public ChangePassword(username: string, oldPassword: string, newPassword: string) {
    const oldPwd = (oldPassword === null || oldPassword === undefined) ? '' : oldPassword;
    const newPwd = (newPassword === null || newPassword === undefined) ? '' : newPassword;

    const endPointUrl: string = this.webApi.EndPoint + '/Authentication/ChangePassword';
    const parameters = new HttpParams()
        .set('username', username)
        .set('oldPassword', oldPwd)
        .set('newPassword', newPwd);
    return this.httpClient.post(endPointUrl, '', { params: parameters });
}

而我的 web api 是

[HttpPost]
    public void ChangePassword(string userName, string oldPassword, string newPassword)
    {
        WebServiceFault fault = _securityManager.ChangePassword(userName, oldPassword, newPassword);
        if (fault == null)
        {
            return;
        }

        throw WebApiServiceFaultHelper.CreateFaultException(fault);
    }

当我将 null 作为旧密码和新密码的参数传递给 ChangePassword() 方法时,我在获取空字符串的 web api 方法中将字符串设为 null。

【问题讨论】:

  • Suren 在下面指出了一些我会同意的细节。我将成为房间里的大象并问:您将空字符串传递给控制器​​的用例究竟是什么?做string.IsNullOrEmpty 检查还不够吗?还是您需要在代码中同时使用 null"" 案例?
  • null 和 " " 有不同的含义。 null 表示用户没有正确调用 api," " 表示用户要设置空密码。
  • 对它们有不同的用例是很合理的。我无法确定的是,您允许用户设置空密码..?如果你问我,那只是糟糕的设计。是否有任何理由为什么您需要让您的用户设置空密码?
  • 无论哪种方式,在反序列化您的值时区分string.Emptynullthis answer 会有帮助吗?

标签: javascript c# angular typescript asp.net-web-api


【解决方案1】:

您正在执行 post 请求,因此请考虑通过 body 作为第二个参数传递数据,而不是作为查询参数。

public ChangePassword(username: string, oldPassword: string, newPassword: string) {
    const oldPwd = (oldPassword === null || oldPassword === undefined) ? '' : oldPassword;
    const newPwd = (newPassword === null || newPassword === undefined) ? '' : newPassword;

    const endPointUrl: string = this.webApi.EndPoint + '/Authentication/ChangePassword';

    return this.httpClient.post(endPointUrl, { username, oldPwd, newPwd });
}

并使用[FromBody] 属性进入API。

[HttpPost]
public void ChangePassword([FromBody]string userName, [FromBody]string oldPassword, [FromBody]string newPassword)
{
    WebServiceFault fault = _securityManager.ChangePassword(userName, oldPassword, newPassword);
    if (fault == null)
    {
        return;
    }

    throw WebApiServiceFaultHelper.CreateFaultException(fault);
}

此外,最好有一个模型来描述更改密码模型并将数据作为模型而不是单独的参数获取。类似的东西

[HttpPost]
public void ChangePassword([FromBody]ChangePasswordModel model)

【讨论】:

  • 感谢您的回答。但是您知道尝试解决核心问题的任何方法。即,将一个空字符串传递给 web api 方法?
  • 如果你传递一个空字符串,它将作为一个空字符串到达​​。
  • 是的,这就是主要问题。如果您在客户端 ChangePassword 方法中观察字符串是否为 null 或未定义,我将传递空字符串。 Web api 仍然收到 null。
  • 你在 API 中得到任何数据吗?
  • 我认为它为空。
【解决方案2】:

不要作为单独的参数传递,而是使用如下模型,

public class ChangePasswordModel 
{
     [DisplayFormat(ConvertEmptyStringToNull = false)]
     public string userName { get; set; }
     [DisplayFormat(ConvertEmptyStringToNull = false)]
     public string oldPassword { get; set; }
     [DisplayFormat(ConvertEmptyStringToNull = false)]
     public string newPassword { get; set; }
}

把它当做,

[HttpPost]
public void ChangePassword([FromBody]ChangePasswordModel model)

模型类的属性上的DisplayFormat 属性会做你想做的事。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 2020-05-05
    • 2018-07-11
    • 2021-07-04
    相关资源
    最近更新 更多