• vue3 编写.netcore可视组件


    一、打包vue项目

    npm run build

    二、新建类库:ClassLibrary2

    将vue.js build目录拷贝到类库www目录下,并设置为嵌入资源

     

     Class1.cs

    1. using Microsoft.AspNetCore.Builder;
    2. using Microsoft.AspNetCore.Http;
    3. using Microsoft.Extensions.FileProviders.Embedded;
    4. using System.Text;
    5. namespace ClassLibrary1
    6. {
    7. public static class Class1
    8. {
    9. public static IApplicationBuilder UseCustomMap(this IApplicationBuilder app)
    10. {
    11. app.Map("/index", HandleMapMainDoc);
    12. app.MapWhen(c =>
    13. {
    14. return c.Request.Path.Value.StartsWith("/assets/");
    15. }, HandleMapJsCss);
    16. //保存产品Api
    17. app.Map("/api/Product", app =>
    18. {
    19. app.Run(async context =>
    20. {
    21. context.Response.StatusCode = 200;
    22. context.Response.ContentType = "application/json";
    23. await context.Response.WriteAsync("{code:0}");
    24. });
    25. });
    26. return app;
    27. }
    28. static void HandleMapMainDoc(IApplicationBuilder app)
    29. {
    30. app.Run(async context =>
    31. {
    32. var resourceNames = typeof(Class1).Assembly.GetManifestResourceNames();
    33. var stream = typeof(Class1).Assembly.GetManifestResourceStream("ClassLibrary2.www.index.html");
    34. var buff = new byte[stream.Length];
    35. stream.Read(buff, 0, buff.Length);
    36. var text = UTF8Encoding.UTF8.GetString(buff);
    37. context.Response.StatusCode = 200;
    38. context.Response.ContentType = "text/html; charset=utf-8";
    39. await context.Response.WriteAsync(text);
    40. });
    41. }
    42. static void HandleMapJsCss(IApplicationBuilder app)
    43. {
    44. app.Run(async context =>
    45. {
    46. var resourceNames = typeof(Class1).Assembly.GetManifestResourceNames();
    47. var path = context.Request.Path.Value;
    48. var stream = typeof(Class1).Assembly.GetManifestResourceStream($"ClassLibrary2.www{path.Replace("/", ".")}");
    49. var buff = new byte[stream.Length];
    50. stream.Read(buff, 0, buff.Length);
    51. var text = UTF8Encoding.UTF8.GetString(buff);
    52. context.Response.StatusCode = 200;
    53. if (path.EndsWith(".js"))
    54. {
    55. context.Response.ContentType = "application/javascript";
    56. }
    57. if (path.EndsWith(".css"))
    58. {
    59. context.Response.ContentType = "text/css";
    60. }
    61. await context.Response.WriteAsync(text);
    62. });
    63. }
    64. }
    65. }

    三、项目中引用ClassLibrary2

    并在Startup.cs中添加一行

      app.UseCustomMap();

    在浏览器输入:http://localhost:5000/index#/

     

     

  • 相关阅读:
    LeetCode——29. 两数相除
    mysql文档--myisam存储引擎--myisam引擎全解--底层探索
    攻防世界1-misc
    vue3 + vite + ts + setup , 第十一练 Vue3自定义全局函数和变量,vue3 如何使用自定义插件
    net-java-php-python-小学随班就读管理系统设计计算机毕业设计程序
    单区域OSPF配置
    关于seata启动时连接数据库异常,Mysql版本8.0
    QT绘图设备
    【计网 传输层概述】 中科大郑烇老师笔记 (十)
    电子器件 电阻参数与选型
  • 原文地址:https://blog.csdn.net/xiaoxionglove/article/details/125471735