[英]How can Pass string as parameter in Web Api C#
How can I pass string parameter in Web API 如何在Web API中传递字符串参数
{"Message":"No HTTP resource was found that matches the request URI ' http://localhost:60698/api/values/GetBynamed/sudeesh3 '.","MessageDetail":"No action was found on the controller 'Values' that matches the request."} {“ Message”:“未找到与请求URI'http:// localhost:60698 / api / values / GetBynamed / sudeesh3 '相匹配的HTTP资源。”,“ MessageDetail”:“在控制器'Values上未找到任何操作'符合要求。“}
public IHttpActionResult getbynamed(string name)
{
List<ImgModel> list1 = new List<ImgModel>();
ImgModel mod = new ImgModel();
SqlConnection con = new SqlConnection(cs);
if (con.State == ConnectionState.Closed) con.Open();
SqlCommand cmd = new SqlCommand("select * from tbl_details where name='" + name + "'", con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
mod.id = Convert.ToInt32(dr[0].ToString());
mod.img = dr[1].ToString();
mod.name = dr[2].ToString();
mod.phone = dr[3].ToString();
list1.Add(mod);
}
return Ok(list1);
}
else
{
return NotFound();
}
}
You need add route configure for API 您需要为API添加路由配置
[Route("api/values/getbynamed/{name}")]
public IHttpActionResult getbynamed(string name)
{
}
use this one: 使用这个:
[Route("api/values/getbynamed/{name}"]
public IHttpActionResult getbynamed(string name)
{
//Do something
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.