我創建的web API工作正常,因爲我檢查了這個問題,但問題是當我在asp.net網站中調用web API時,這裏是下面調用Web API的代碼在asp .net web窗體中調用web API的錯誤狀態碼404
protected void btn_search_Click(object sender, EventArgs e)
{
HClient.BaseAddress = new Uri("http://localhost:50653/");
HClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
BoiMember obj = new BoiMember();
obj.CustomerId = txt_customerid.Text.Trim();
obj.AadhaarNo = txt_aadharno.Text.Trim();
obj.CustomerName = txt_name.Text.Trim();
obj.AccountNo = txt_accountno.Text.Trim();
obj.MobileNo = txt_mobile.Text.Trim();
obj.branchcd = Session["BranchCode"].ToString();
obj.ZoneCode = Session["ZoneCode"].ToString();
obj.Campcd = "1";
obj.ind = 1;
obj.SourceType = 2;
obj.UserId = Session["UserName"].ToString();
string uri = "api/BoiMember/GetRecord/";
var response = HClient.GetAsync(uri+obj).Result;
if (response.IsSuccessStatusCode)
{
var GetData = response.Content.ReadAsAsync<IEnumerable<BoiMember>>().Result;
GvdRecords.DataSource = GetData;
GvdRecords.DataBind();
}
else
{
}
}
凡在名爲BoiMemberController當我把這個網絡API不帶參數它工作正常,但我傳遞參數,我得到了狀態碼404錯誤的API控制器未找到。我的網絡APIConfig.cs有一個代碼
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
你爲什麼要做'uri + obj'?這不會產生有效的網址。它產生了什麼? – Nkosi
即使你將'obj'序列化爲一個使用過的'uri +'/'+ obj'的字符串,它也沒什麼意義 - 你會如何使用它? –