0
從MVC控制器調用的Web API POST方法返回下面的錯誤:調用Web API Post方法錯誤「遠程服務器返回錯誤:(405)不允許的方法」
"The remote server returned an error: (405) Method Not Allowed"
下面MVC控制器動作代碼調用一個輔助類的方法和在調試時,我可以看到控制移動從這裏到下一個方法:
public ActionResult Submit(FormCollection form)
{
Lead_Options lead = new Lead_Options();
lead.Situation = form.GetValue("InsuranceFor").AttemptedValue;
lead.State = form.GetValue("InsuranceState").AttemptedValue;
Uri url= Url_Helper.GetUri(BaseUrl, service1+"Post"); // returns http://localhost:52985/api/HealthInsurance/Post
string obj= new JavaScriptSerializer().Serialize(lead);
Object data = WebApi_Helper.PostData(url,obj);
return RedirectToAction("Parameters");
}
「WebApi_Helper.PostData」的Helper類,被起訴的通用方法來調用Web API:
public static string PostData(Uri url,string obj)
{
string data = null;
try
{
using (WebClient proxy = new WebClient())
{
proxy.Headers.Add(HttpRequestHeader.Accept, "application/json");
data = proxy.UploadString(url,"Post", obj); //Here got error
}
}
catch (Exception ex)
{
throw ex;
}
return data;
}
下面的WebAPI方法的代碼,但在調試時要求不來這裏都
[HttpPost]
public void Post(string lead)
{
//leadOptService.AddListOptions(lead);
}
請告訴我我在做什麼錯誤以及如何解決。