ASP.NET Core 使用中间件实现带UI的Lib

mac2024-11-13  8

 

styleSheet.css

body { } button { background-color: blue; color: white; font-weight: bold }

htmlpage.html

<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <!--引用Js--> <script type="text/javascript" src="/lib/jq.js"></script> <!--引用Css--> <link href="/lib/style.css" rel="stylesheet" /> <script type="text/javascript"> $(function () { $("#btnOk").click(function () { alert("ok"); }); }); </script> </head> <body> <button id="btnOk">Click</button> </body> </html>

jquery.min.js

......

Startup.cs

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System.Reflection; namespace Rou.Demo { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); //jquery.min.js app.Map("/lib/jq.js", HandleMapJs); //htmlpage.html app.Map("/lib/index.html", HandleMapHtml); //styleSheet.css app.Map("/lib/style.css", HandleMapCss); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); } /// <summary> /// jquery.min.js /// </summary> /// <param name="app"></param> private void HandleMapJs(IApplicationBuilder app) { app.Run(async context => { var js =await GetResourceAsStringAsync("Rou.Demo.Source.Js.jquery.min.js"); context.Response.ContentType = "application/javascript"; await context.Response.WriteAsync(js); await context.Response.CompleteAsync(); }); } /// <summary> /// htmlpage.html /// </summary> /// <param name="app"></param> private void HandleMapHtml(IApplicationBuilder app) { app.Run(async context => { var html =await GetResourceAsStringAsync("Rou.Demo.Source.Htmls.htmlpage.html"); context.Response.ContentType = "text/html; charset=utf-8"; await context.Response.WriteAsync(html); await context.Response.CompleteAsync(); }); } /// <summary> /// styleSheet.css /// </summary> /// <param name="app"></param> private void HandleMapCss(IApplicationBuilder app) { app.Run(async context => { var html = await GetResourceAsStringAsync("Rou.Demo.Source.Css.styleSheet.css"); context.Response.ContentType = "text/css; charset=utf-8"; await context.Response.WriteAsync(html); await context.Response.CompleteAsync(); }); } /// <summary> /// 获取资源文件内容 /// </summary> /// <param name="name"></param> /// <returns></returns> private async Task<string> GetResourceAsStringAsync(string name) { using (var stream = typeof(Startup).Assembly.GetManifestResourceStream(name)) { var buffer = new byte[stream.Length]; await stream.ReadAsync(buffer, 0, buffer.Length); var content = System.Text.Encoding.UTF8.GetString(buffer); return content; } } } }

运行效果:

 

最新回复(0)