Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将值作为空字符串从typescript传递给WEb API_Javascript_C#_Angular_Typescript_Asp.net Web Api - Fatal编程技术网

Javascript 将值作为空字符串从typescript传递给WEb API

Javascript 将值作为空字符串从typescript传递给WEb API,javascript,c#,angular,typescript,asp.net-web-api,Javascript,C#,Angular,Typescript,Asp.net Web Api,我想从javascript向C#web api传递一个空字符串。 但是当我从typescript传递空字符串时,我在webapi参数中得到null。 如何传递空字符串 以下是我的客户端web api调用: public ChangePassword(username: string, oldPassword: string, newPassword: string) { const oldPwd = (oldPassword === null || oldPassword === und

我想从javascript向C#web api传递一个空字符串。 但是当我从typescript传递空字符串时,我在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);
    }

当将NoLL作为旧密码和新密码的参数传递给CuxEasWord()/Case>方法时,我得到的Web API方法中的字符串是空的,得到的是空字符串。

< p>您正在做一个POST请求,因此考虑将正文作为第二个参数传递,而不是作为查询参数。

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)

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

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)

模型类属性上的属性将实现您想要的功能。

谢谢您的回答。但你知道任何尝试解决核心问题的方法。i、 将空字符串传递给web api方法?如果传递空字符串,它将作为空字符串到达。是的,这是主要问题。如果在client ChangePassword方法中观察到字符串是否为null或未定义,我将传递空字符串。web api仍然收到空值。您在api中得到任何数据吗?我得到的是空值。Suren在下面指出了一些细节,我同意。我将成为房间里的大象,并问:将空字符串传递给控制器的用例是什么?是否正在执行
字符串检查。是否NullOrEmpty
检查不够?或者您需要在代码中同时使用
null
大小写吗?null和“”有不同的含义。null定义用户未正确调用api,而“”定义用户希望设置空密码。您有不同的使用案例是合理的。我不能理解的是,你允许用户设置一个空密码。。?如果你问我,那只是个糟糕的设计。您是否有任何理由需要让用户设置空密码?无论哪种方式,在反序列化值时区分
string.empty
null
都会有所帮助?