当前位置: 首页 > news >正文

【Vue】vue2与netcore webapi跨越问题解决

系列文章

C#底层库–记录日志帮助类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/124187709

文章目录

  • 系列文章
  • 前言
  • 一、技术介绍
  • 二、问题描述
  • 三、问题解决
    • 3.1 方法一:前端Vue修改
    • 3.2 方法二:后端允许Cors跨越访问
  • 四、资源链接


前言

本专栏为【H5】,主要介绍前端知识点。
在这里插入图片描述

一、技术介绍

跨越介绍

二、问题描述

vue2与netcore webapi跨越问题解决
在这里插入图片描述

三、问题解决

3.1 方法一:前端Vue修改

配置代理转发,加URL拦截,转换成新的URL
vue.config.js文件,加一下代码:

    proxy:{'/api': { // api表示拦截以/api开头的请求路径target: 'http://localhost:5296/api', //跨域的域名changeOrigin:true,//是否开启跨域pathRewrite:{ '^/api':'' // 重写请求,把/api变为空字符}}}

3.2 方法二:后端允许Cors跨越访问

Program.cs文件

//配置跨域
builder.Services.AddCors(c =>
{c.AddDefaultPolicy(policy =>{policy.AllowAnyOrigin()//允许所有来源的访问.AllowAnyHeader()//允许所有类型的请求头.AllowAnyMethod();//允许所有类型的请求});
});

启动跨越,在run之前

app.UseCors();
app.UseHttpsRedirection();

四、资源链接

vue.config.js文件

'use strict'
const path = require('path')
const defaultSettings = require('./src/settings.js')function resolve(dir) {return path.join(__dirname, dir)
}const name = defaultSettings.title || 'vue Admin Template' // page title// If your port is set to 80,
// use administrator privileges to execute the command line.
// For example, Mac: sudo npm run
// You can change the port by the following methods:
// port = 9528 npm run dev OR npm run dev --port = 9528
const port = process.env.port || process.env.npm_config_port || 9528 // dev port// All configuration item explanations can be find in https://cli.vuejs.org/config/
module.exports = {/*** You will need to set publicPath if you plan to deploy your site under a sub path,* for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,* then publicPath should be set to "/bar/".* In most cases please use '/' !!!* Detail: https://cli.vuejs.org/config/#publicpath*/publicPath: '/',outputDir: 'dist',assetsDir: 'static',lintOnSave: process.env.NODE_ENV === 'development',productionSourceMap: false,devServer: {port: port,open: true,overlay: {warnings: false,errors: true},proxy:{'/api': { // api表示拦截以/api开头的请求路径target: 'http://localhost:5296/api', //跨域的域名changeOrigin:true,//是否开启跨域pathRewrite:{ '^/api':'' // 重写请求,把/api变为空字符}}}},configureWebpack: {// provide the app's title in webpack's name field, so that// it can be accessed in index.html to inject the correct title.name: name,resolve: {alias: {'@': resolve('src')}}},chainWebpack(config) {// it can improve the speed of the first screen, it is recommended to turn on preloadconfig.plugin('preload').tap(() => [{rel: 'preload',// to ignore runtime.js// https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],include: 'initial'}])// when there are many pages, it will cause too many meaningless requestsconfig.plugins.delete('prefetch')// set svg-sprite-loaderconfig.module.rule('svg').exclude.add(resolve('src/icons')).end()config.module.rule('icons').test(/\.svg$/).include.add(resolve('src/icons')).end().use('svg-sprite-loader').loader('svg-sprite-loader').options({symbolId: 'icon-[name]'}).end()// set preserveWhitespaceconfig.module.rule('vue').use('vue-loader').loader('vue-loader').tap(options => {options.compilerOptions.preserveWhitespace = truereturn options}).end()config.when(process.env.NODE_ENV !== 'development',config => {config.plugin('ScriptExtHtmlWebpackPlugin').after('html').use('script-ext-html-webpack-plugin', [{// `runtime` must same as runtimeChunk name. default is `runtime`inline: /runtime\..*\.js$/}]).end()config.optimization.splitChunks({chunks: 'all',cacheGroups: {libs: {name: 'chunk-libs',test: /[\\/]node_modules[\\/]/,priority: 10,chunks: 'initial' // only package third parties that are initially dependent},elementUI: {name: 'chunk-elementUI', // split elementUI into a single packagepriority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or apptest: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm},commons: {name: 'chunk-commons',test: resolve('src/components'), // can customize your rulesminChunks: 3, //  minimum common numberpriority: 5,reuseExistingChunk: true}}})// https:// webpack.js.org/configuration/optimization/#optimizationruntimechunkconfig.optimization.runtimeChunk('single')})}
}

Program.cs文件

using System.Configuration;
using WebApplication5;var builder = WebApplication.CreateBuilder(args);// Add services to the container.//配置跨域
builder.Services.AddCors(c =>
{c.AddDefaultPolicy(policy =>{policy.AllowAnyOrigin()//允许所有来源的访问.AllowAnyHeader()//允许所有类型的请求头.AllowAnyMethod();//允许所有类型的请求});
});builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();var app = builder.Build();app.UseCors();
app.UseHttpsRedirection();if (app.Environment.IsDevelopment())
{app.UseSwagger();app.UseSwaggerUI();
}app.UseHttpsRedirection();app.UseAuthorization();app.MapControllers();app.Run();
http://www.lryc.cn/news/192647.html

相关文章:

  • SpringSecurity + jwt + vue2 实现权限管理 , 前端Cookie.set() 设置jwt token无效问题(已解决)
  • 【21】c++设计模式——>装饰模式
  • 【博客707】模版化拆解并获取victoriametrics的metricsql各个元素
  • nodejs + express 实现 http文件下载服务程序
  • Qt多文本编辑器项目实战
  • CVE-2017-7529 Nginx越界读取内存漏洞
  • 力扣每日一题136:只出现一次的数字
  • 导航栏参考代码
  • 区块链(11):java区块链项目之页面部分实现
  • RootSIFT---SIFT图像特征的扩展
  • ChatGPT角色扮演教程,Prompt词分享
  • zabbix监控——自定义监控内容
  • 中断机制-中断协商机制、中断方法
  • three.js入门 —— 实现第一个3D案例
  • 《动手学深度学习 Pytorch版》 8.4 循环神经网络
  • 什么是物联网阀控水表?
  • Kafka 开启SASL/SCRAM认证 及 ACL授权(一)认证
  • 关于智能控制领域中模糊控制算法的概述
  • 剖析伦敦银最新价格走势图
  • 通用人工智能技术(深度学习,大模型,Chatgpt,多模态,强化学习,具身智能)
  • makefile的特性-部分语法记录
  • 【Java 进阶篇】JavaScript 正则表达式(RegExp)详解
  • 51单片机之串口通信例程
  • Hadoop高可用集群(HA)一键启动脚本
  • C#开发的OpenRA游戏之金钱系统(1)
  • Puppeteer监听网络请求、爬取网页图片(二)
  • GoLang连接mysql数据库
  • 软件工程与计算总结(八)软件设计基础
  • someip 入门
  • C# 使用Parallel去执行并行下载