我已经创建了一个 web api 项目,在值控制器中我创建了一个方法 InsertHeading,它接受三个参数并返回一个唯一的 id。该方法如下所示:-
public int InsertHeading([FromBody]string appid, [FromBody]string type, [FromBody]string detail)
{
int x = 1;
return 1;
}
我也试过这个变种
[HttpPost]
public int InsertHeading(string appid, string type, string detail)
{
int x = 1;
return 1;
}
当我 从soap UI 中提供如下网址时,这段代码正在运行:- http://server:port/LoggingAPi/Values/InsertHeading 。
但是,当我尝试从我的 c# 代码中调用此方法时,我遇到了 404 错误。这是我尝试过的两种方法:-
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://xxxxx:45422/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var append = new MasterLogInfo() { appid = "2", type = "request", detail = "test call from genesys" };
HttpResponseMessage response = await client.PostAsJsonAsync("LoggingAPI/Values/InsertMasterloginfo", append);
if (response.IsSuccessStatusCode)
{
// Get the URI of the created resource.
Uri finalURL = response.Headers.Location;
}
}
方法2:-
// client.BaseAddress = new Uri("http://localhost:53117/");
client.BaseAddress = new Uri("http://xxxxxx:45422/");
client.DefaultRequestHeaders.Accept.Clear();
// client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
var user = new MasterLogInfo();
user.appid = "100";
user.typeRequest = "Test";
user.detail = "test call from genesys";
var response = client.PostAsync("LoggingAPI/Values/InsertMasterloginfo", new StringContent(new JavaScriptSerializer().Serialize(user), Encoding.UTF8, "application/json")).Result;
// var response = client.PostAsync("Values/InsertHeading", new StringContent(new JavaScriptSerializer().Serialize(user), Encoding.UTF8, "application/json")).Result;
if (response.IsSuccessStatusCode)
{
// Get the URI of the created resource.
Uri finalURL = response.Headers.Location;
}
}
如果我在参数中使用 FromBody 标记,我得到 500 内部服务器错误,没有它我得到 404 错误。谁能告诉我我错过了什么。出于安全目的,我删除了插入标题的正文