当前位置:   article > 正文

Net Core API +Vue Nginx集成发布_net core usestaticfiles vue

net core usestaticfiles vue

1、在net core api  Startup.cs 中的Configure中设置默api默认页面 关键代码 app.UseFileServer

  1. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  2. {
  3. if (env.IsDevelopment())
  4. {
  5. app.UseDeveloperExceptionPage();
  6. }
  7. #if DEBUG
  8. app.UseStaticFiles(); //开启访问静态文件/wwwroot目录文件,要放在UseRouting前面
  9. #else
  10. app.UseStaticFiles(new StaticFileOptions()
  11. {
  12. ServeUnknownFileTypes = true,
  13. FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(
  14. Path.Combine(Path.GetDirectoryName(typeof(Startup).Assembly.Location), "wwwroot")),//wwwroot相当于真实目录
  15. });
  16. app.UseFileServer(
  17. new FileServerOptions()
  18. {
  19. FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider( Path.Combine(Path.GetDirectoryName(typeof(Startup).Assembly.Location), "wwwroot")),
  20. EnableDirectoryBrowsing = true,
  21. DefaultFilesOptions ={
  22. DefaultFileNames = new List<string>(){ "/index.html" }
  23. },
  24. EnableDefaultFiles = true
  25. });
  26. #endif
  27. app.UseHttpsRedirection();
  28. app.UseRouting();
  29. app.UseAuthorization();
  30. app.UseEndpoints(endpoints =>
  31. {
  32. endpoints.MapControllers();
  33. });
  34. }

详细使用即讲解,参见官方文档:ASP.NET Core 中的静态文件 | Microsoft Docs

2、vue 项目路由配置base(vite.config.js)(注:本人使用vue3.0)

关键代码:base: '/certManage/', (certManage为nginx设置的代理名称,此处为示例)

注:api接口前缀也需要添加/certManage前缀,各位根据各自实际情况配置,此处不做过多描述

  1. import { defineConfig, loadEnv } from 'vite'
  2. import path from 'path'
  3. import createVitePlugins from './vite/plugins'
  4. // https://vitejs.dev/config/
  5. export default defineConfig(({ mode, command }) => {
  6. const env = loadEnv(mode, process.cwd())
  7. const alias = {
  8. // 设置路径
  9. '~': path.resolve(__dirname, './'),
  10. // 设置别名
  11. '@': path.resolve(__dirname, './src')
  12. }
  13. if (command === 'serve') {
  14. // 解决警告You are running the esm-bundler build of vue-i18n.
  15. alias['vue-i18n'] = 'vue-i18n/dist/vue-i18n.cjs.js'
  16. }
  17. return {
  18. plugins: createVitePlugins(env, command === 'build'),
  19. resolve: {
  20. // https://cn.vitejs.dev/config/#resolve-alias
  21. alias: alias,
  22. // 导入时想要省略的扩展名列表
  23. // https://cn.vitejs.dev/config/#resolve-extensions
  24. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
  25. dedupe: ['vue']
  26. },
  27. css: {
  28. devSourcemap: true //开发模式时启用
  29. },
  30. base: '/certManage/',
  31. // 打包配置
  32. build: {
  33. sourcemap: command === 'build' ? false : 'inline',
  34. outDir: 'dist', //指定输出目录
  35. assetsDir: 'assets', //指定静态资源存储目录(相对于outDir)
  36. // 将js、css文件分离到单独文件夹
  37. rollupOptions: {
  38. output: {
  39. chunkFileNames: "static/js/[name]-[hash].js",
  40. entryFileNames: "static/js/[name]-[hash].js",
  41. assetFileNames: "static/[ext]/[name]-[hash].[ext]"
  42. }
  43. },
  44. },
  45. // vite 相关配置
  46. server: {
  47. port: 8887,
  48. host: true,
  49. open: true,
  50. proxy: {
  51. // https://cn.vitejs.dev/config/#server-proxy
  52. '/dev-api': {
  53. target: 'http://192.168.10.188:9092/certManage',
  54. changeOrigin: true,
  55. rewrite: (path) => path.replace(/^\/dev-api/, '')
  56. },
  57. // '/fileView': {
  58. // target: 'http://192.168.10.188:9092/fileView',
  59. // changeOrigin: true,
  60. // },
  61. // '/msghub': {
  62. // target: 'http://localhost:8888',
  63. // ws: true,
  64. // rewrite: (path) => path.replace(/^\/msgHub/, '')
  65. // }
  66. },
  67. },
  68. }
  69. })

3、nginx配置

4、net core api接口发布并在IIS中部署(部署略)

5、发布vue项目,并将发布包中的文件拷贝至 api接口发布包下的wwwroot目录下

 

6、启动nginx

7、使用配置的nginx代理地址,在浏览器中访问http://192.168.10.188:9092/certManage 

效果如下:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/386790?site
推荐阅读
相关标签
  

闽ICP备14008679号