C# 如何在webapi中使用get-as方法参数
为了在post man中使用查询字符串参数来获取单个数据,我希望在尝试此操作时使用get方法作为参数 公共HttpResponseMessage测试(int id)显示错误 未找到与请求URI“”匹配的HTTP资源。 未找到与名为“test”的控制器匹配的类型。 代码是:C# 如何在webapi中使用get-as方法参数,c#,asp.net-web-api,postman,webapi,C#,Asp.net Web Api,Postman,Webapi,为了在post man中使用查询字符串参数来获取单个数据,我希望在尝试此操作时使用get方法作为参数 公共HttpResponseMessage测试(int id)显示错误 未找到与请求URI“”匹配的HTTP资源。 未找到与名为“test”的控制器匹配的类型。 代码是: public class DefaultController: ApiController { public object Conn { get; private set;} [Route("api/t
public class DefaultController: ApiController {
public object Conn { get; private set;}
[Route("api/test/id")]
[HttpGet]
public HttpResponseMessage test(int id) {
DataSet ds = new DataSet();
string conname = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
SqlConnection conn = new SqlConnection(conname);
conn.Open();
SqlCommand cmd = new SqlCommand("select *from Employees", conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(ds);
return new HttpResponseMessage {
Content = new StringContent(JsonConvert.SerializeObject(ds), Encoding.UTF8, "application/json")
};
}
}
您的
id
是路线中的可变参数,路线看起来不正确。尝试使用{}环绕id。如下所示
[Route("api/test/{id}")] // <===== notice that id is in {}
[HttpGet]
public HttpResponseMessage test(int id)
查看更多详细信息如果这解决了您的问题,请将其标记为答案。
[Route("api/test/{id:int}")]