至於OData的部分, 請參考貓大的文章來做, 記得先用http://www.nuget.org/packages/Microsoft.AspNet.WebApi.OData所提到的方法來安裝OData喔.
全部Source Code如下(C#)_測試用所以沒刪除一些用不到的東東
兩個類別範例
Product.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SelfHost
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
}
}
EmployeesInfo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SelfHost
{
public class EmployeesInfo
{
String m_WorkNo;
string m_Name;
public string WorkNo
{
get
{
return m_WorkNo;
}
set
{
m_WorkNo = value;
}
}
public string Name
{
get
{
return m_Name;
}
set
{
m_Name = value;
}
}
}
}
啟動程式
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Http;
using System.Web.Http.SelfHost;
namespace SelfHost
{
class Program
{
static void Main(string[] args)
{
var config = new HttpSelfHostConfiguration("http://localhost:8080");
config.Routes.MapHttpRoute(
"API Default", "api/{controller}/{id}",
new { id = RouteParameter.Optional });
using (HttpSelfHostServer server = new HttpSelfHostServer(config))
{
server.OpenAsync().Wait();
Console.WriteLine("Press Enter to quit.");
Console.ReadLine();
}
}
}
}
Controller
ProductsController.cs
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Net;
using System.Web.Http;
namespace SelfHost
{
public class ProductsController : ApiController
{
Product[] products = new Product[]
{
new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
};
[Queryable]
public IQueryable<EmployeesInfo> GetAllProducts()
{
List<EmployeesInfo> EmpLists;
EmpLists=ReadEmplyeesData();
return EmpLists.AsQueryable();
}
public Product GetProductById(int id)
{
var product = products.FirstOrDefault((p) => p.Id == id);
if (product == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return product;
}
public IEnumerable<Product> GetProductsByCategory(string category)
{
return products.Where(p => string.Equals(p.Category, category,
StringComparison.OrdinalIgnoreCase));
}
static private string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file, using the
// System.Configuration.ConfigurationSettings.AppSettings property
return "Data Source = EXExxxx;Initial catalog = Gxxxx;" +
"User id = sa; Password = Gxxxx";
}
private static List<EmployeesInfo> ReadEmplyeesData()
{
List<EmployeesInfo> EmpLists = new List<EmployeesInfo>();
EmployeesInfo tEMPL;
string connectionString = GetConnectionString();
string queryString =
"SELECT * FROM dbo.Employees;";
using (SqlConnection connection = new SqlConnection(
connectionString))
{
SqlCommand command = new SqlCommand(
queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
try
{
while (reader.Read())
{
tEMPL = new EmployeesInfo();
tEMPL.WorkNo = reader[1].ToString();
tEMPL.Name = reader[16].ToString();
EmpLists.Add(tEMPL);
}
}
finally
{
// Always call Close when done reading.
reader.Close();
}
return EmpLists;
}
}
}
}
P.S.撈資料庫的範例請自行改寫,否則無法執行喔!
執行成功後, 建議按照貓大的方法用Chrome Postman來測試比較方便!
沒有留言:
張貼留言