简体   繁体   English

从MVC web api中的存储过程返回json

[英]Return in json from stored procedure in MVC web api

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未找到与请求匹配的 HTTP 资源

MessageDetail: no action was found on the controller 'AutoComplete' that matches the request. MessageDetail:在与请求匹配的控制器“AutoComplete”上找不到任何操作。

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)请提出一种将数据集从存储过程返回到 json 结果的方法(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.您可以像这样返回一个 JsonResult 而不是返回一个 List 。

[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 !您的Controller名称是Version而不是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但正如 OP 在评论中提到的,如果你想像这样调用你的端点: 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:那么您需要修改您的 webapi 操作参数以采用id而不是term ,如下所示:

[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 .在此更改之后,您应该能够以http://localhost:5555/api/Version/acCompany/somevalue 的形式调用它。 When this is invoked, id will take in somevalue.当它被调用时,id 将接受一些值。

The reason why you need to use id is because the way how they are configured in webapiconfig.cs.您需要使用 id 的原因是因为它们在 webapiconfig.cs 中的配置方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM