请问为什么我的web api应用不能访问单个查询,每次调用都是全部数据出来

sunck1970 2015-06-10 03:50:26
模型定义 Brand.sc
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TerminalApp.Models
{
public class Brand
{
public string machine_brand { get; set; }
}
}

控制器BrandsController.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TerminalApp.Models;

namespace TerminalApp.Controllers
{
public class BrandsController : ApiController
{

public IEnumerable<Brand> GetAllBrands()
{
string sql = "select machine_brand from terminal_models_brand order by machine_brand";
DataTable BrandTable = PublicFunction.GetVehicleData(sql);



//查询表达式的写法
var AllBrands = from dt1 in BrandTable.AsEnumerable() //查询
select new Brand { machine_brand = dt1["machine_brand"].ToString() };
return AllBrands;
}

public IHttpActionResult GetBrand(string abrand)
{

string queryString = "select machine_brand from terminal_models_brand where machine_brand='" + abrand + "' order by machine_brand";
DataTable BrandTable = PublicFunction.GetVehicleData(queryString);
if (BrandTable == null)
{
return NotFound();
}

var OneBrand = from dt1 in BrandTable.AsEnumerable() //查询
select new Brand { machine_brand = dt1["machine_brand"].ToString() };
return Ok(OneBrand);

}


}
}

测试时使用的路径是http://localhost:50710/api/brands/LG

与http://localhost:50710/api/brands

返回的结果是一样的,都是把所有记录调用出来了,而不是设想的,第一句应该只调用LG的一条记录



通过设置断点可以看出,访问地址http://localhost:50710/api/brands/LG

根本就没有访问public IHttpActionResult GetBrand(string abrand) 这个函数

请问这是什么原因?



谢谢
...全文
335 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunck1970 2015-06-10
  • 打赏
  • 举报
回复
歪打正着搜到一篇文章,原来是对路由的理解不对 url: "{controller}/{action}/{id}" 原来ID是默认的参数名称,如果参数是其他名称,就必须把参数写出来在url中 所以正确URL是 http://localhost:50710/api/brands?abrand=LG

12,162

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧