我在前端写了一个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 文件不要少了。
如果也帮助到了你,请点个赞哈,欢迎评论区留言。