调用此API方法时出现错误404 这是服务器代码
public class employeesController : ApiController
{
public HttpResponseMessage CreateEmployee(string strName, string DOB, string Salary, string Photo, string bIsActive, string strExectutedBy)
{
HRServices.EntityFramework.WebAPIEntitiesDb db = new EntityFramework.WebAPIEntitiesDb();
try
{
byte[] arrFileData = Convert.FromBase64String(Photo);
int nValue = db.CREATE_EMPLOYEE(strName, Convert.ToDateTime(DOB), Convert.ToDecimal(Salary), arrFileData, Convert.ToBoolean(bIsActive), strExectutedBy);
db.Dispose();
return Request.CreateResponse(HttpStatusCode.OK, "SUCCESS");
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}
}
这是客户端代码:
try
{
string strURL = "http://localhost/HRServices/api/employees/CreateEmployee";
byte[] arrFileData = File.ReadAllBytes("d:\\123.jpg");
string FileData = Convert.ToBase64String(arrFileData);
string strParams = "strName=Imad&DOB=2019-5-25&Salary=15000&Photo=" + FileData + "&bIsActive=true&strExecutedBy=admin";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(strURL);
request.ContentType = "application/json";
request.Method = "POST";
byte[] arrData = Encoding.UTF8.GetBytes(strParams);
request.ContentLength = arrData.Length;
Stream sw = request.GetRequestStream();
sw.Write(arrData,0,arrData.Length);
sw.Flush();
sw.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string strResult = new StreamReader(response.GetResponseStream()).ReadToEnd();
response.Close();
response.Dispose();
response = null;
}
catch(WebException ex)
{
MessageBox.Show(ex.Message);
}
在检查请求详细信息时,我发现了以下详细信息:throghu Katalon或任何其他REST测试工具 { “ $ type”:“ System.Web.Http.HttpError,System.Web.Http”, “ message”:“未找到与请求URI'http://localhost/HRServices/api/employees/CreateEmployee'匹配的HTTP资源。”, “ messageDetail”:“未在与名称'CreateEmployee'匹配的控制器'雇员'上找到任何动作。” }