如何在VS Code中为ASP.NET Core项目实现jQuery DataTables服务端处理
项目需求与现有配置
我在Visual Studio Code中开发ASP.NET Core项目,已在MySQL数据库中存入对应业务数据,但当前无法为这些数据库数据应用jQuery DataTables插件,无法以该插件的服务端处理模式在浏览器中渲染展示数据表,需要实现该功能的具体落地方法。
项目现有文件结构与代码
Models层实体定义
Models目录下共3个核心实体类,代码如下:
Course.cs
using System.ComponentModel.DataAnnotations.Schema; namespace ContosoUniversity.Models { public class Course { [DatabaseGenerated(DatabaseGeneratedOption.None)] public int CourseID {get;set;} public string Title {get;set;} public int Credits {get;set;} public ICollection<Enrollment> Enrollments {get;set;} } }
Enrollment.cs
{ public enum Grade { A, B, C, D, F } public class Enrollment { public int EnrollmentID {get;set;} public int CourseID {get;set;} public int StudentID{get;set;} public Grade? Grade {get;set;} public Course Course {get;set;} public Student Student {get;set;} } }
Student.cs
using System.Collections.Generic; namespace ContosoUniversity.Models { public class Student { public int ID {get;set;} public String LastName {get;set;} public String FirstName {get;set;} public DateTime EnrollmentDate {get;set;} public ICollection<Enrollment> Enrollments { get; set; } } }
共享布局视图
文件路径:Pages/Shared/Layout.cshtml
<html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>@ViewData["Title"] - Contoso University</title> <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" /> <link rel="stylesheet" href="~/css/site.css" /> </head> <body> <header> <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3"> <div class="container"> <a class="navbar-brand" asp-area="" asp-page="/Index">ContosoUniversity</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between"> <ul class="navbar-nav flex-grow-1"> <li class="nav-item"> <a class="nav-link text-dark" asp-area="" asp-page="/About">About</a> </li> <li class="nav-item"> <a class="nav-link text-dark" asp-area="" asp-page="/Students/Index">Students</a> </li> <li class="nav-item"> <a class="nav-link text-dark" asp-area="" asp-page="/Courses/Index">Courses</a> </li> <li class="nav-item"> <a class="nav-link text-dark" asp-area="" asp-page="/Teachers/Index">Teachers</a> </li> <li class="nav-item"> <a class="nav-link text-dark" asp-area="" asp-page="/Departments/Index">Departments</a> </li> </ul> </div> </div> </nav> </header> <div class="container"> <main role="main" class="pb-3"> @RenderBody() </main> </div> <footer class="border-top footer text-muted"> <div class="container"> © 2021 - Contoso University - <a asp-area="" asp-page="/Privacy">Privacy</a> </div> </footer> <script src="~/lib/jquery/dist/jquery.min.js"></script> <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script> <script src="~/js/site.js" asp-append-version="true"></script> @await RenderSectionAsync("Scripts", required: false) </body> </html>
项目配置文件
ContosoUniversity.csproj 现有配置:
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>net5.0</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.0-*" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.0-*"> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <PrivateAssets>all</PrivateAssets> </PackageReference> <PackageReference Include="Microsoft.EntityFrameworkCore.SQLite" Version="5.0.0-*" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0-*" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.0-*"> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <PrivateAssets>all</PrivateAssets> </PackageReference> <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.0-*" /> <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.1" /> <PackageReference Include="Microsoft.Composition" Version="1.0.30" ExcludeAssets="All" /> <PackageReference Include="System.Composition" Version="1.0.31" /> </ItemGroup> </Project>
调试配置文件
.vscode/launch.json 现有配置:
{ // 使用IntelliSense查看可用属性 // 悬停可获取现有属性的描述信息 "version": "0.2.0", "configurations": [ { "name": ".NET Core Launch (web)", "type": "coreclr", "request": "launch", "preLaunchTask": "build", "program": "${workspaceFolder}/bin/Debug/<net5.0>/<ContosoUniversity.dll>", "args": [], "cwd": "${workspaceFolder}", "externalConsole": true, "stopAtEntry": false, "serverReadyAction": { "action": "openExternally", "pattern": "\\bNow listening on:\\s+(https?://\\S+)" }, "env": { "ASPNETCORE_ENVIRONMENT": "Development" }, "sourceFileMap": { "/Views": "${workspaceFolder}/Views" } }, { "name": ".NET Core Launch (web)", "type": "coreclr", "request": "launch", "preLaunchTask": "build", "program": "${workspaceFolder}/bin/Debug/net5.0/ContosoUniversity.dll", "args": [], "cwd": "${workspaceFolder}", "stopAtEntry": false, "serverReadyAction": { "action": "openExternally", "pattern": "\\bNow listening on:\\s+(https?://\\S+)" }, "env": { "ASPNETCORE_ENVIRONMENT": "Development" }, "sourceFileMap": { "/Views": "${workspaceFolder}/Views" } }, { "name": ".NET Core Attach", "type": "coreclr", "request": "attach", "processId": "${command:pickProcess}" } ] }
启动类配置
Startup.cs 现有配置:
using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace ContosoUniversity { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // 运行时调用此方法,向服务容器添加依赖 public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); } // 运行时调用此方法,配置HTTP请求处理管道 public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); // 生产环境默认HSTS有效期为30天,可根据实际需求调整 app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); }); } } }
首页视图
Pages/Index.cshtml 现有代码:
@model IndexModel @{ ViewData["Title"] = "Home page"; } <div class="row mb-auto"> <div class="col-md-4"> <div class="row no-gutters border mb-4"> <div class="col p-4 mb-4"> <p class="card-text"> Contoso University 是一个演示如何在ASP.NET Core Razor Pages应用中使用Entity Framework Core的简单示例项目。 </p> </div> </div> </div> <div class="col-md-4"> <div class="row no-gutters border mb-4"> <div class="col p-4 d-flex flex-column position-static"> <p class="card-text mb-auto"> 可通过系列教程步骤搭建完整应用。 </p> <p> <a href="/" class="streched-link">查看对应教程</a> </p> </div> </div> </div> <div class="col-md-4"> <div class="row no-gutters border mb-4"> <div class="col p-4 d-flex flex-column"> <p class="card-text mb-auto"> 可通过公开渠道获取完整项目源码 </p> <p> <a href="/" class="streched-link">项目源码 </a> </p> </div> </div> </div> </div>
注:原页面中指向外部站点的跳转链接已替换为本地占位路径,符合无外跳要求。
内容的提问来源于stack exchange,提问作者SHASHANKSHEKHAR SINGH
相关产品推荐
相关产品推荐

