【问题标题】:Return in json from stored procedure in MVC web api从 MVC Web api 中的存储过程返回 json
【发布时间】:2020-03-04 20:56:27
【问题描述】:
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;
    }
}

以下属性

public class AutoCompleteCompany
{
    public string Value { get; set; }
}

将数据集转换为列表

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

Webapi配置

config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(name: "DefaultApi",
                routeTemplate: "Api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional });

错误:

未找到与请求匹配的 HTTP 资源

MessageDetail:在控制器“自动完成”上找不到与请求匹配的操作。

以下自定义方法有效

public string GetAccess(string id)
{
    return "value3";
}

请建议一种方法将数据集从存储过程返回到 json 作为结果(web api rest)

【问题讨论】:

标签: c# asp.net-mvc asp.net-web-api


【解决方案1】:

你必须调用类似:

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

您的 Controller 名称是 Version 而不是 AutoComplete

【讨论】:

    【解决方案2】:

    因为您的控制器名称是版本,而不是自动完成。你只是使用了错误的网址。

    【讨论】:

      【解决方案3】:

      将您的返回类型更改为 IActionResult,并将您的列表包装在 OkObjectResult 中,就像这样...

      return Ok(co);
      

      【讨论】:

        【解决方案4】:

        您可以像这样返回 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 {);
        }
        

        【讨论】:

          【解决方案5】:

          我知道这已通过查询字符串得到回答。但正如 OP 在 cmets 中提到的,如果你想像这样调用你的端点:http://localhost:5555/api/Version/acCompany/somevalue

          那么您需要修改您的 webapi 操作参数以采用 id 而不是 term,如下所示:

          [HttpGet]
          public List<AutoCompleteCompany> acCompany(string id)
          {
             // rest of your action code would make use of id instead of term
          }
          

          在此更改之后,您应该能够以http://localhost:5555/api/Version/acCompany/somevalue 调用它。当这个被调用时,id 会接受一些值。

          之所以需要使用 id 是因为它们在 webapiconfig.cs 中的配置方式。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-12-29
            • 2022-07-31
            • 2022-01-06
            相关资源
            最近更新 更多