1
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)

2
  • which url endpoint you are trying to use when you received the error MessageDetail: no action was found on the controller 'AutoComplete' that matches the request.? Commented Mar 4, 2020 at 21:17
  • ocalhost:5555/api/Version/acCompany/somevalue.............this got error Commented Mar 4, 2020 at 21:38

5 Answers 5

1

You must call something like :

http://yourhostname/Api/Version/acCompany?term=someString

Your Controller name is Version not AutoComplete!

Sign up to request clarification or add additional context in comments.

Comments

1

Because your controller name is Version, not AutoComplete. You just use wrong url.

Comments

1

Change your return type to IActionResult, and wrap your list in an OkObjectResult, like so...

return Ok(co);

Comments

1

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 {);
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.