Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Web API in dhtmlxGantt with ASP.NET Core

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to implement Web API in dhtmlxGantt with ASP.NET Core". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to implement Web API in dhtmlxGantt with ASP.NET Core".

Implement Web API

Now it's time for the actual REST API implementation. Go to Startup.cs and enable MVC routing (if not already enabled):

Startup file

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 ();}

Add Controller

Create the Controllers folder and create three empty API Controller: one for Tasks, one for Links, and one for the entire dataset:

Task controller

Let's create a controller for Tasks. It will define basic CRUD operations for the Gantt task.

How does this work:

In a GET request, the task is loaded from the database and the output is the data transfer object of the task

In a PUT / POST request, the task comes from the client as a WebAPITask class. They are represented in this way in dhtmlxGantt. Therefore, you should convert them to the format of the EntityFramework (task class) data model. You can then save the changes in DatabaseContext.

Controller / TaskController.csusing 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 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"});}

Link controller

Next, you should create a controller for Links:

Controller / LinkController.csusing 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 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"});}}

Data controller

Finally, you need to create a controller for the data operation:

Controller / DataController.csusing 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)};}

We're all set. You can run the application and view the complete Gantt.

At this point, I believe you have a deeper understanding of "how to implement Web API in dhtmlxGantt with ASP.NET Core". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report