如何使用 WebRequest(无模型)将对象传递给外部 Web Api ASP.NET MVC
How to pass objects using WebRequest (without model) to External Web Api ASP.NET MVC
我有一个 asp.net MVC 和外部 WEBAPI。我想按以下顺序传递数据:
- 从Ajax到MVC控制器
- 从 MVC 控制器到外部 WEBAPI
- 外部 WEBAPI 发送响应
- MVC 控制器收到响应
- MVC 控制器 return 响应 AJAX
问题:
它可以保存到数据库,但所有的值都是 0。似乎我传递的对象在某处丢失了,但我不知道在哪里。
真的需要这方面的帮助。谢谢
下面是我的 Ajax 调用 MVC 控制器的代码:
function savePostApplication(theData) {
var insert = {};
insert.JobTitleId = JobTitleId;
insert.Division = Division;
insert.SalaryScaleId = SalaryScaleId;
insert.DepartmentId = DepartmentId;
insert.MinistryId = MinistryId;
$.post("/Advert/AddAdvertApplication", theData,
function (response) {
alert(response);
});
}
这是我的 MVC 控制器代码:
public String AddAdvertApplication(JSON data)
{
var url = "https://localhost:44331/api/jobadvertisementdetails";
var wr = WebRequest.Create(url);
wr.Method = "POST";
string json = JsonConvert.SerializeObject(data);
byte[] byteArray = Encoding.UTF8.GetBytes(json);
wr.ContentType = "application/x-www-form-urlencoded";
wr.ContentLength = byteArray.Length;
var dataStream = wr.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = wr.GetResponse();
var Status = ((HttpWebResponse)response).StatusDescription;
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
这是我的 WEBAPI 代码:
public String PostJobAdvertisementDetail(JobAdvertisementDetail jobAdvertisementDetail)
{
try
{
DataTable table = new DataTable();
String query = @"insert into dbo.JobAdvertisementDetails (JobTitleId, Division, SalaryScaleId, DepartmentId, MinistryId) values (" + jobAdvertisementDetail.JobTitleId +
@"," + jobAdvertisementDetail.Division + "," + jobAdvertisementDetail.SalaryScaleId +
@","+jobAdvertisementDetail.DepartmentId+","+jobAdvertisementDetail.MinistryId+")";
using(var con = new SqlConnection(ConfigurationManager.ConnectionStrings["CONN"].ConnectionString))
using(var cmd = new SqlCommand(query, con))
using(var da = new SqlDataAdapter(cmd))
{
cmd.CommandType = CommandType.Text;
da.Fill(table);
}
return "Data Saved";
}
catch (Exception ex)
{
return "Error";
}
}
更新:
现在可以了。我只是将内容类型从“application/x-www-form-urlencoded”更改为“application/json”
我有一个 asp.net MVC 和外部 WEBAPI。我想按以下顺序传递数据:
- 从Ajax到MVC控制器
- 从 MVC 控制器到外部 WEBAPI
- 外部 WEBAPI 发送响应
- MVC 控制器收到响应
- MVC 控制器 return 响应 AJAX
问题: 它可以保存到数据库,但所有的值都是 0。似乎我传递的对象在某处丢失了,但我不知道在哪里。
真的需要这方面的帮助。谢谢
下面是我的 Ajax 调用 MVC 控制器的代码:
function savePostApplication(theData) {
var insert = {};
insert.JobTitleId = JobTitleId;
insert.Division = Division;
insert.SalaryScaleId = SalaryScaleId;
insert.DepartmentId = DepartmentId;
insert.MinistryId = MinistryId;
$.post("/Advert/AddAdvertApplication", theData,
function (response) {
alert(response);
});
}
这是我的 MVC 控制器代码:
public String AddAdvertApplication(JSON data)
{
var url = "https://localhost:44331/api/jobadvertisementdetails";
var wr = WebRequest.Create(url);
wr.Method = "POST";
string json = JsonConvert.SerializeObject(data);
byte[] byteArray = Encoding.UTF8.GetBytes(json);
wr.ContentType = "application/x-www-form-urlencoded";
wr.ContentLength = byteArray.Length;
var dataStream = wr.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = wr.GetResponse();
var Status = ((HttpWebResponse)response).StatusDescription;
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
这是我的 WEBAPI 代码:
public String PostJobAdvertisementDetail(JobAdvertisementDetail jobAdvertisementDetail)
{
try
{
DataTable table = new DataTable();
String query = @"insert into dbo.JobAdvertisementDetails (JobTitleId, Division, SalaryScaleId, DepartmentId, MinistryId) values (" + jobAdvertisementDetail.JobTitleId +
@"," + jobAdvertisementDetail.Division + "," + jobAdvertisementDetail.SalaryScaleId +
@","+jobAdvertisementDetail.DepartmentId+","+jobAdvertisementDetail.MinistryId+")";
using(var con = new SqlConnection(ConfigurationManager.ConnectionStrings["CONN"].ConnectionString))
using(var cmd = new SqlCommand(query, con))
using(var da = new SqlDataAdapter(cmd))
{
cmd.CommandType = CommandType.Text;
da.Fill(table);
}
return "Data Saved";
}
catch (Exception ex)
{
return "Error";
}
}
更新: 现在可以了。我只是将内容类型从“application/x-www-form-urlencoded”更改为“application/json”