您好,登录后才能下订单哦!
本篇内容主要讲解“带有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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。