本篇內(nèi)容主要講解“帶有ASP.NET Core的dhtmlxGantt怎么實(shí)施Web API”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“帶有ASP.NET Core的dhtmlxGantt怎么實(shí)施Web API”吧!
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名申請(qǐng)、網(wǎng)頁空間、營銷軟件、網(wǎng)站建設(shè)、昌平網(wǎng)站維護(hù)、網(wǎng)站推廣。
實(shí)施Web API
現(xiàn)在該進(jìn)行實(shí)際的REST API實(shí)施了。轉(zhuǎn)到Startup.cs并啟用MVC路由(如果尚未啟用):
啟動(dòng)文件
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); } //The method is called by the runtime. Use it to configure HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseDefaultFiles(); app.UseStaticFiles(); app.UseMvc(); }
添加控制器
創(chuàng)建Controllers文件夾并創(chuàng)建三個(gè)空的API Controller:一個(gè)用于Tasks,另一個(gè)用于Links,另一個(gè)用于整個(gè)數(shù)據(jù)集:
任務(wù)控制器
讓我們?yōu)門asks創(chuàng)建一個(gè)控制器。它將為甘特任務(wù)定義基本的CRUD操作。
這個(gè)怎么運(yùn)作:
在GET請(qǐng)求中,任務(wù)是從數(shù)據(jù)庫中加載的,輸出是任務(wù)的數(shù)據(jù)傳輸對(duì)象;
在PUT / POST請(qǐng)求中,任務(wù)作為WebAPITask類來自客戶端。它們?cè)赿htmlxGantt中以這種方式表示。因此,您應(yīng)該將它們轉(zhuǎn)換為EntityFramework(任務(wù)類)的數(shù)據(jù)模型的格式。之后,可以將更改保存在DatabaseContext中。
控制器/ TaskController.cs using System.Collections.Generic; using System.Linq; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Mvc; using DHX.Gantt.Models; namespace DHX.Gantt.Controllers { [Produces("application/json")] [Route("api/task")] public class TaskController : Controller { private readonly GanttContext _context; public TaskController(GanttContext context) { _context = context; } // GET api/task [HttpGet] public IEnumerableGet() { return _context.Tasks .ToList() .Select(t => (WebApiTask)t); } // GET api/task/5 [HttpGet("{id}")] public WebApiTask Get(int id) { return (WebApiTask)_context .Tasks .Find(id); } // POST api/task [HttpPost] public ObjectResult Post(WebApiTask apiTask) { var newTask = (Task)apiTask; _context.Tasks.Add(newTask); _context.SaveChanges(); return Ok(new { tid = newTask.Id, action = "inserted" }); } // PUT api/task/5 [HttpPut("{id}")] public ObjectResult Put(int id, WebApiTask apiTask) { var updatedTask = (Task)apiTask; var dbTask = _context.Tasks.Find(id); dbTask.Text = updatedTask.Text; dbTask.StartDate = updatedTask.StartDate; dbTask.Duration = updatedTask.Duration; dbTask.ParentId = updatedTask.ParentId; dbTask.Progress = updatedTask.Progress; dbTask.Type = updatedTask.Type; _context.SaveChanges(); return Ok(new { action = "updated" }); } // DELETE api/task/5 [HttpDelete("{id}")] public ObjectResult DeleteTask(int id) { var task = _context.Tasks.Find(id); if (task != null) { _context.Tasks.Remove(task); _context.SaveChanges(); } return Ok(new { action = "deleted" }); } } }
鏈接控制器
接下來,您應(yīng)該為Links創(chuàng)建一個(gè)控制器:
控制器/LinkController.cs using System.Collections.Generic; using System.Linq; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Mvc; using DHX.Gantt.Models; namespace DHX.Gantt.Controllers { [Produces("application/json")] [Route("api/link")] public class LinkController : Controller { private readonly GanttContext _context; public LinkController(GanttContext context) { _context = context; } // GET api/Link [HttpGet] public IEnumerableGet() { return _context.Links .ToList() .Select(t => (WebApiLink)t); } // GET api/Link/5 [HttpGet("{id}")] public WebApiLink Get(int id) { return (WebApiLink)_context .Links .Find(id); } // POST api/Link [HttpPost] public ObjectResult Post(WebApiLink apiLink) { var newLink = (Link)apiLink; _context.Links.Add(newLink); _context.SaveChanges(); return Ok(new { tid = newLink.Id, action = "inserted" }); } // PUT api/Link/5 [HttpPut("{id}")] public ObjectResult Put(int id, WebApiLink apiLink) { var updatedLink = (Link)apiLink; updatedLink.Id = id; _context.Entry(updatedLink).State = EntityState.Modified; _context.SaveChanges(); return Ok(new { action = "updated" }); } // DELETE api/Link/5 [HttpDelete("{id}")] public ObjectResult DeleteLink(int id) { var Link = _context.Links.Find(id); if (Link != null) { _context.Links.Remove(Link); _context.SaveChanges(); } return Ok(new { action = "deleted" }); } } }
數(shù)據(jù)控制器
最后,您需要為數(shù)據(jù)操作創(chuàng)建一個(gè)控制器:
控制器/DataController.cs using System.Collections.Generic; using System.Linq; using Microsoft.AspNetCore.Mvc; using DHX.Gantt.Models; namespace DHX.Gantt.Controllers { [Produces("application/json")] [Route("api/data")] public class DataController : Controller { private readonly GanttContext _context; public DataController(GanttContext context) { _context = context; } // GET api/data [HttpGet] public object Get() { return new { data = _context.Tasks.ToList().Select(t => (WebApiTask)t), links = _context.Links.ToList().Select(l => (WebApiLink)l) }; } } }
都準(zhǔn)備好了。您可以運(yùn)行該應(yīng)用程序,并查看完整的Gantt。
到此,相信大家對(duì)“帶有ASP.NET Core的dhtmlxGantt怎么實(shí)施Web API”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!