您好,登錄后才能下訂單哦!
本篇內容主要講解“帶有ASP.NET Core的dhtmlxGantt怎么實施Web API”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“帶有ASP.NET Core的dhtmlxGantt怎么實施Web API”吧!
實施Web API
現在該進行實際的REST API實施了。轉到Startup.cs并啟用MVC路由(如果尚未啟用):
啟動文件
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddDbContext<GanttContext>(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(); }
添加控制器
創建Controllers文件夾并創建三個空的API Controller:一個用于Tasks,另一個用于Links,另一個用于整個數據集:
任務控制器
讓我們為Tasks創建一個控制器。它將為甘特任務定義基本的CRUD操作。
這個怎么運作:
在GET請求中,任務是從數據庫中加載的,輸出是任務的數據傳輸對象;
在PUT / POST請求中,任務作為WebAPITask類來自客戶端。它們在dhtmlxGantt中以這種方式表示。因此,您應該將它們轉換為EntityFramework(任務類)的數據模型的格式。之后,可以將更改保存在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 IEnumerable<WebApiTask> Get() { 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" }); } } }
鏈接控制器
接下來,您應該為Links創建一個控制器:
控制器/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 IEnumerable<WebApiLink> Get() { 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" }); } } }
數據控制器
最后,您需要為數據操作創建一個控制器:
控制器/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) }; } } }
都準備好了。您可以運行該應用程序,并查看完整的Gantt。
到此,相信大家對“帶有ASP.NET Core的dhtmlxGantt怎么實施Web API”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。