ASP.NET 웹 API에서 컨트롤러를 만들었습니다. 다음은 컨트롤러 아래ASP.NET MVC 4의 WEB API에서 함수 가져 오기 요청 게시
public class CalculateController : ApiController
{
public IEnumerable<string> Get()
{
return new string[] { "from get method" };
}
public IEnumerable<string> Post([FromBody]string value)
{
return new string[] { "from post method" };
}
}
의 코드 내가 webAPI
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create("http://localhost:62479/api/calculate");
StringBuilder postdata = new StringBuilder("value=Anshuman");
byte[] data = Encoding.UTF8.GetBytes(postdata.ToString());
httpWReq.Method = "POST";
httpWReq.ContentType = "application/x-www-form-urlencoded";
httpWReq.ContentLength = data.Length;
using (Stream stream = httpWReq.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
에 POST 요청을 만들기 위해 사용하고있는 코드입니다
문제는 내가 POST 요청을 할 경우에도 것입니다 그 경우에도 데이터는 컨트롤러의 GET 메소드에서 반환됩니다. Web API 프로젝트의 기본 구성을 변경하지 않았습니다. MVC 4를 사용 중입니다.
시간 내 주셔서 감사합니다. 다른 정보가 필요한 경우 의견을 추가하십시오.
동일한 컴퓨터에서 Visual Studio 2012로 실행되는 두 프로젝트가 있습니다.
액션 이름 -http를 제공하는 명시 적으로 시도해보십시오 경로로 가져 오기 조치로 이동되는 게시물을 제한 할 관련 행동 속성 : // localhost를 : 62,479/api/calculate/Post – malkam
@malkam이 작동하지 않습니다. 또한 기본 URL 경로 구성에는 URL에 작업 이름이 포함되지 않습니다. –