vue 路由放到IIS上部署,API .net 6.0 生成站点,报 404解决

我在前端写了一个vue项目,配置了一个路由:如下:

import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'


const routes = [

  {

    path: '/',

    redirect: '/new',

  },

    {

      path:'/new/:id?',

      name:'newpage',

      component:()=>import('../views/NewPage.vue'),

    }

]

const router = createRouter({

  history: createWebHistory(),

  routes

})

export default router

然后,在vs code 中使用调用正常。

API是用 .net 6.0开发的WEBAPI

发布到IIS上后,报错信息

找不到此 localhost 页面

找不到 Web 地址对应的网页: http://localhost:8003/new/20

HTTP ERROR 404

后来尝试了很多种方法,看了很多文档。

最后,解决了。

如是.net core 6

直接在Program.cs 中添加

app.UseRouting();

app.UseEndpoints(endpoints =>
{
    // API 路由
    endpoints.MapControllers();

    // 为Vue应用配置的路由,将所有其他路径都重定向到index.html
    endpoints.Map("{*any}", async context =>
    {
        await context.Response.SendFileAsync(app.Environment.WebRootPath + "/index.html");
    });
});

如果是 .net core 3 

// Startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
     
 
    // 配置API路由
    app.UseRouting();
 
    app.UseEndpoints(endpoints =>
    {
        // API 路由
        endpoints.MapControllers();
 
        // 为Vue应用配置的路由,将所有其他路径都重定向到index.html
        endpoints.Map("{*any}", async context =>
        {
            await context.Response.SendFileAsync(env.WebRootPath + "/index.html");
        });
    });
    
 
}

然后,再发布到IIS,就成功了。web.config 文件不要少了。

如果也帮助到了你,请点个赞哈,欢迎评论区留言。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值