调用WebApi方法,找不到错误404

时间:2017-12-13 07:33:04

标签: asp.net-mvc vb.net api

我遇到WebMethod MVC的问题 这是控制器中的方法

[HttpPost]        
public IHttpActionResult GetData(int Company,
    int FromYear,int ToYear, int language ,
    int DATA_SERIES_TYPE, bool PERIOD_Q1)
{
    var ds = new DataSet();
    try
    {
        ds = Actions.GetValues(Company, FromYear, ToYear,DATA_SERIES_TYPE);
        return Ok(ds);
    }
    catch(Exception ex)
    {
        return InternalServerError(ex);
    }
}

我在API中使用此方法,当我使用webrequest

从另一个项目调用api时
Dim Request As WebRequest = WebRequest.Create("http://localhost/PBWebApi/api/GetData")
With strJson
    .Append("{")
    .Append($"Company:{_dynaSolver.CompanyCode.ToString()}" & ",")
    .Append($"FromYear:{_dynaSolver.FromYear.ToString}" & ",")
    .Append($"ToYear:{_dynaSolver.ToYear.ToString}" & ",")
    .Append($"language:{CType(_dynaSolver.DataLanguage, Integer).ToString}" & ",")
    .Append($"DATA_SERIES_TYPE:{ CType(_dynaSolver.Fundamentals, Integer).ToString}")
    .Append("}")
End With

Dim data = Encoding.UTF8.GetBytes(strJson.ToString)
With Request
    .Method = "POST"
    .ContentType = "application/json; charset=utf-8"
    .ContentLength = data.Length
End With

Dim stream = Request.GetRequestStream()
stream.Write(data, 0, data.Length)
stream.Close()

Dim response = Request.GetResponse().GetResponseStream
Dim reader As StreamReader = New StreamReader(response)
Dim res = reader.ReadToEnd()
dsRes = JsonConvert.DeserializeObject(Of DataSet)(res)
reader.Close()
response.Close()

当我执行此操作时,响应会出现此错误。

  

未找到与请求URI匹配的HTTP资源

我尝试在另一个函数中只使用一个参数,但我得到了相同的结果。 我尝试使用fiddler,但我得到了同样的错误。

1 个答案:

答案 0 :(得分:0)

I fixed the problem creating and object called id and adding all the properties to that object. After that i send the object as json in the content and it works. Regards