.Net3.1 Asp.net core 使用Vue

16 篇文章 2 订阅

版本

.net 3.1
vue3.0

Vue设置

vue设置很简单,写完程序后运行打包命令:

npm run build

生成了静态文件,目录如下:
在这里插入图片描述

Asp.net Core设置

将vue生成的静态文件全部复制到wwwroot目录下,没有就创建一个
在这里插入图片描述
在Startup.cs的Configure中添加如下代码:

using Microsoft.AspNetCore.Http;
using System.IO;

	app.UseDefaultFiles();  //默认访问index.html
	app.UseStaticFiles();   //使用wwwroot下的静态文件
	
	// 防止刷新页面报404
	app.Run(async (context) =>
	    {
			context.Response.ContentType = "text/html";
			await context.Response.SendFileAsync(Path.Combine(env.WebRootPath, "index.html"));
		});

跨域

如果vue中访问的是此.net core的api,还需要设置跨域:
vue.config.js中设置:

module.exports = {
    publicPath:'/',    // 公共路径
    outputDir: process.env.NODE_ENV === "development" ? 'devdist' : 'dist', // 根据生产环境更换打包路径
    assetsDir: "assets",
    lintOnSave: false,  
    devServer: {
        proxy: {
            '/api': {
                target: 'http://localhost:5137/api/',
                ws: true,
                changeOrigin: true,
                pathRewrite: {  
                    '^/api': ''
                }

            }
        }
    }
};

Startup.cs中的ConfigureServices方法中添加:

 services.AddCors(options =>
            {
                options.AddDefaultPolicy(
                    builder =>
                    {
                        builder.WithOrigins("*");
                    });
            });

Configure添加

app.UseCors();
  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值