public class VersionController : ApiController
{
[HttpGet]
public List<AutoCompleteCompany> acCompany(string term)
{
DataSet ds = new DataSet();
List<AutoCompleteCompany> co= new List<AutoCompleteCompany>();
try
{
ds = getdetails(term);//privae method returns dataset
co = ds.Tables[0].ToList<AutoCompleteCompany>();
}
catch (Exception ex)
{
}
return co;
}
}
Properties below
public class AutoCompleteCompany
{
public string Value { get; set; }
}
Converts dataset to list
public static List<T> ToList<T>(this DataTable table) where T : new()
{
IList<PropertyInfo> properties = typeof(T).GetProperties().ToList();
List<T> result = new List<T>();
foreach (var row in table.Rows)
{
var item = CreateItemFromRow<T>((DataRow)row, properties);
result.Add(item);
}
return result;
}
private static T CreateItemFromRow<T>(DataRow row, IList<PropertyInfo> properties) where T : new()
{
T item = new T();
foreach (var property in properties)
{
if (property.PropertyType == typeof(System.DayOfWeek))
{
DayOfWeek day = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), row[property.Name].ToString());
property.SetValue(item, day, null);
}
else
{
if (row[property.Name] == DBNull.Value)
property.SetValue(item, null, null);
else
property.SetValue(item, row[property.Name], null);
}
}
return item;
}
Webapiconfig
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(name: "DefaultApi",
routeTemplate: "Api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional });
Error:
No HTTP resource was found that matches the request
MessageDetail: no action was found on the controller 'AutoComplete' that matches the request.
and below custom method works
public string GetAccess(string id)
{
return "value3";
}
Please suggest a way to return dataset from stored procedure in to json as result (web api rest)
Because your controller name is Version, not AutoComplete. You just use wrong url.
将您的返回类型更改为 IActionResult,并将您的列表包装在 OkObjectResult 中,就像这样......
return Ok(co);
Instead of returning a List you can just return a JsonResult like this.
[HttpGet]
public JsonResult acCompany(string term)
{
DataSet ds = new DataSet();
List<AutoCompleteCompany> co= new List<AutoCompleteCompany>();
try
{
ds = getdetails(term);//privae method returns dataset
co= ds.Tables[0].ToList<AutoCompleteCompany>();
}
catch (Exception ex)
{
}
return Json(new { co {);
}
You must call something like :
http://yourhostname/Api/Version/acCompany?term=someString
Your Controller
name is Version
not AutoComplete
!
I know this has been answered via query string. But as mentioned by OP in comments, if you want to invoke your endpoint like this: http://localhost:5555/api/Version/acCompany/somevalue
then you will need to modify your webapi action parameter to taken in id
instead of term
as shown below:
[HttpGet]
public List<AutoCompleteCompany> acCompany(string id)
{
// rest of your action code would make use of id instead of term
}
After this change, you should be able to invoke it as http://localhost:5555/api/Version/acCompany/somevalue . When this is invoked, id will take in somevalue.
The reason why you need to use id is because the way how they are configured in webapiconfig.cs.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.