如何將Devexpress MVC Template加入Asp.Net MVC Web API功能
1.需加入的參考
a.Nuget取得項目
Newtonsoft.json
Microsoft.AspNet.Web.Optimization
(須從Nuget上取得Json.Net)
System.Net.Http
System.Net.Http.Formatting
System.Net.Http.WebRequest
System.Web.Http
System.Web.WebHost
2.增加App_Start資料夾, 及增加以下四個檔案(可參考asp.net mvc webapi專案樣本)
a.BundleConfig.cs
b.FilterConfig.cs
c.RouteConfig.cs
d.WebApiConfig.cs ( * ) <=重點
public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } }
3. 修改Global.asax
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
ModelBinders.Binders.DefaultBinder = new DevExpress.Web.Mvc.DevExpressEditorsBinder();
}
4. 在Controller裡面新增一個測試用的web api controller
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
// POST api/values
public void Post([FromBody]string value)
{
}
// PUT api/values/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
public void Delete(int id)
{
}
}
5. 測試網址 http://localhost:xxx/api/values,如果有看到有回傳結果(json format),就成功了。