当我尝试在我的 web api 上发布一个帖子时,我收到错误 404,但我不确定为什么我会得到它的 url,而且一切都是正确的。
http://10.0.1.96/testwebapi/api/case/UpdateCasePersonal/?id=4584&forename=Andy&surname=Wilson&<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="244149454d4819415c454954484164415c45495448410a474b0a514f" rel="noreferrer noopener nofollow">[email protected]</a>&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
});
}
最佳答案
如果您确实想发送 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)
{
...
}
你的网址应该像这样:
http://10.0.1.96/testwebapi/api/case/UpdateCasePersonal/4584/Mr/Andy/Wilson/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="13766b727e637f7653766b727e637f763d707c3d6678" rel="noreferrer noopener nofollow">[email protected]</a>/0166%20254%204876/0733333333/
这不是一个好的做法。
这完全暴露了您在请求中的数据。一般来说,串联几乎从来都不是执行与数据相关的任何操作的最佳方式。您应该将数据作为一个整体发送给调用。像这样的东西:
[HttpPut]
[Route("updatecasepersonal/{CaseId}"]
public string UpdateCasePersonal(string Caseid, RequestDto request)
{
...
}
当然,RequestDto
应该是您创建的一个类,它需要所有这些字段:标题、名字、姓氏、电子邮件等,并将其传递到您的 Javascript 中(或者您发送帖子的任何地方) )。并且它应该命名为适合您的要求的名称。因为这看起来像用户个人资料数据,或者类似的东西,比如 ContactDto
。
关于C# Web API POST 时出现错误 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45219786/