简体   繁体   English

如何在Web Api C#中将字符串作为参数传递

[英]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.

相关问题 C#Web API如何将URL作为参数传递 - c# web api How to pass a url as parameter 如何将 json 字符串传递给 web api c# - How to pass json string to web api c# C# / ASP.NET Core Web API如何传递参数? - C# / ASP.NET Core Web API : how to pass a list parameter? 如何通过清单 <int> Web Api C#上SOAP UI的参数 - How to Pass List<int> parameter to SOAP UI on a Web Api C# 如何将C#类型为int / double / string / int []的参数传递给本地C? - How to pass parameter that can be of type int/double/string/int[] from C# to native C? 通过字典 <string,string> Web API中的参数 - Pass a Dictionary<string,string> parameter in web api 将 JavaScript 日期传递给 C# Web API 字符串属性 - Pass JavaScript date to C# Web API string property C# WEB API 如何使用参数 HttpPost 以及如何 httpsGet 字符串并返回自定义列表 - C# WEB API How to HttpPost with Parameter and how to httpsGet string and return custom List 如何将日期时间参数作为字符串传递给 Web api 控制器 (httpPut) PutAysnc - How to pass Datetime parameter as string to web api controller (httpPut) PutAysnc 如何创建使用String作为其搜索参数的get方法?(asp.net Web API C#) - How to create get method that uses String as its search parameter?(asp.net web api C#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM