首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >开机自检时出现C# Web API错误404

开机自检时出现C# Web API错误404
EN

Stack Overflow用户
提问于 2017-07-20 23:58:14
回答 1查看 589关注 0票数 2

当我尝试在我的web api上发布一篇文章时,我得到了一个错误404,我不确定为什么我会得到它,url和一切都是正确的。

代码语言:javascript
复制
http://10.0.1.96/testwebapi/api/case/UpdateCasePersonal/?id=4584&forename=Andy&surname=Wilson&email=example@example.co.uk&telephone=0166%20254%204876&mobile=0733333333&title=Mr

这是我的web api代码的Url,接下来我将把它放入

代码语言:javascript
复制
[HttpPost]
[Route("updatecasepersonal/")]
public string UpdateCasePersonal(string Caseid, string Title, string Forename, string Surname, string Telephone, string Email, string Mobile)
{
    using (SqlConnection con = new SqlConnection(conString))
    {
        con.Open();
        var query = $@"UPDATE TestDB.dbo.[crm-data] SET Title=" + Title + ", Forename=" + Forename + ", Surname=" + Surname + ", Telephone=" + Telephone + ", Email=" + Email + ", Mobile=" + Mobile + " WHERE Caseid=" + Caseid;
        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            cmd.CommandType = CommandType.Text;
            var dtb = new DataTable();
            var da = new SqlDataAdapter(cmd);
            da.Fill(dtb);
            return "Done";
        }
    }
}

另外,我这样更新我的表也是正确的吗?或者我做错了所有的事情,因为我还不能流利地使用c#

如果需要,可以提供更多代码

这是我调用api的代码

代码语言:javascript
复制
onUpdateClick(e) {
    this.setState({
        updatedForename: this.state.Case.Forename,
        updatedSurname: this.state.Case.Surname,
        updatedHomeTelephone: this.state.Case.Telephone,
        updatedMobileTelephone: this.state.Case.Mobile,
        updatedEmail: this.state.Case.Email,
        updatedTitle: this.state.titleValue,
        updatedPurpose: this.state.purposeValue,
        updatedMaritalStatus: this.state.maritalValue,
        updatedEmpStatus: this.state.empValue,
    }, function () {

        var id = this.state.Case.Caseid;
        var forename = this.state.updatedForename;
        var surname = this.state.updatedSurname;
        var email = this.state.updatedEmail;
        var homeTelephone = this.state.updatedHomeTelephone;
        var mobileTelephone = this.state.updatedMobileTelephone;
        var title = this.state.updatedTitle;

        axios.post('http://10.0.1.96/testwebapi/api/case/UpdateCasePersonal/', {
            params: {
                id: id,
                forename: forename,
                surname: surname,
                email: email,
                telephone: homeTelephone,
                mobile: mobileTelephone,
                title: title
            }
        }).then(function (res) {
        }).catch(function (err) {
        });

    });
    this.setState({
        hasSaved: true
    });

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-21 00:16:58

如果您确实希望在URL中发送连接的数据,请执行以下操作:

代码语言:javascript
复制
[HttpPut]
[Route("updatecasepersonal/{Caseid}/{Title}/{Forename}/{Surname}/{Email}/{Telephone}/{Mobile}")]
public string UpdateCasePersonal(string Caseid, string Title, string Forename, string Surname, string Telephone, string Email, string Mobile)
{
    ...
}

你的url应该看起来像这样:

代码语言:javascript
复制
http://10.0.1.96/testwebapi/api/case/UpdateCasePersonal/4584/Mr/Andy/Wilson/example@example.co.uk/0166%20254%204876/0733333333/

这不是一个好的实践。

这完全暴露了请求中的数据。一般来说,连接几乎从来都不是做任何与数据相关的事情的最佳方法。您应该将数据作为一个整体发送给调用。类似于:

代码语言:javascript
复制
[HttpPut]
[Route("updatecasepersonal/{CaseId}"]
public string UpdateCasePersonal(string Caseid, RequestDto request)
{
    ...
}

当然,RequestDto应该是您创建的一个类,它需要所有这些字段:标题、名字、姓氏、电子邮件等,并将其传递到您的Javascript中(或在您发送帖子的任何地方)。它应该被命名为适合你的要求的东西。因为这看起来像用户配置文件数据,或者类似于ContactDto的东西。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45219786

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档