当我尝试在我的web api上发布一篇文章时,我得到了一个错误404,我不确定为什么我会得到它,url和一切都是正确的。
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,接下来我将把它放入
[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的代码
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
});
}
发布于 2017-07-21 00:16:58
如果您确实希望在URL中发送连接的数据,请执行以下操作:
[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应该看起来像这样:
http://10.0.1.96/testwebapi/api/case/UpdateCasePersonal/4584/Mr/Andy/Wilson/example@example.co.uk/0166%20254%204876/0733333333/
这不是一个好的实践。
这完全暴露了请求中的数据。一般来说,连接几乎从来都不是做任何与数据相关的事情的最佳方法。您应该将数据作为一个整体发送给调用。类似于:
[HttpPut]
[Route("updatecasepersonal/{CaseId}"]
public string UpdateCasePersonal(string Caseid, RequestDto request)
{
...
}
当然,RequestDto
应该是您创建的一个类,它需要所有这些字段:标题、名字、姓氏、电子邮件等,并将其传递到您的Javascript中(或在您发送帖子的任何地方)。它应该被命名为适合你的要求的东西。因为这看起来像用户配置文件数据,或者类似于ContactDto
的东西。
https://stackoverflow.com/questions/45219786
复制相似问题