模型定义 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) 这个函数
请问这是什么原因?
谢谢