【问题标题】:Problem with routes in hosted web application (VUE.JS + NET CORE 3.1)托管 Web 应用程序中的路由问题(VUE.JS + NET CORE 3.1)
【发布时间】:2022-01-05 08:14:48
【问题描述】:

编辑:

最后,经过几天尝试解决 Axios BaseURL 的问题,似乎我的托管应用程序中出现 404 错误的原因一定是使用 VUE.JS + NET.CORE 3.1 创建的应用程序在建立路由时遇到问题(它在本地主机上完美运行)。我已经更改了问题的主标题并公开了我的 startup.cs 文件,如果有人发现其中有什么奇怪的地方,我将不胜感激。

Startup.cs:

    public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                //Configuration = configuration;
                Configuration = new ConfigurationBuilder()                        
                            .AddJsonFile("appsettings.json")
                            .Build();
            }        
            public IConfiguration Configuration { get; }
            
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddSpaStaticFiles(configuration: options => { options.RootPath = "/";});
                services.AddControllers();
                services.AddHostedService<ApplicationPartsLogger>();            
                services.AddDbContext<DbContextSistema>(options =>
                    options.UseSqlServer(Configuration.GetConnectionString("Conexion"))                
                    );
                services.AddCors(options => {
                    options.AddPolicy("Todos",
                    builder => builder.WithOrigins("*").WithHeaders("*").WithMethods("*").SetIsOriginAllowed((host) => true));
                });
                services.AddMvc(options => options.EnableEndpointRouting = false);
                services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                    .AddJwtBearer(options =>
                    {
                        options.TokenValidationParameters = new TokenValidationParameters
                        {
                            ValidateIssuer = true,
                            ValidateAudience = true,
                            ValidateLifetime = true,
                            ValidateIssuerSigningKey = true,
                            ValidIssuer = Configuration["Jwt:Issuer"],
                            ValidAudience = Configuration["Jwt:Issuer"],                        
                            IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"]))
                        };
                    });            
            }
            
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env,ILoggerFactory loggerFactory)
            {                        
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler();                
                }            
                app.UseStaticFiles();
                app.UseSpaStaticFiles();
                loggerFactory.AddFile("Logs/Log-{Date}.txt");            
                app.UseCors("Todos");
                app.UseRouting();
                app.UseAuthentication();
                app.UseAuthorization();
                app.UseEndpoints(endpoints =>
                    { 
                    endpoints.MapControllerRoute(name:"default",pattern: "{controller=Home}/{action=Index}/{id}");                
                    });
                app.MapWhen(x => !x.Request.Path.Value.StartsWith("/api"), builder =>
                 {
                     builder.UseEndpoints(endpoints =>
                     endpoints.MapFallbackToController("Index", "Home"));
                 });
            }
        }    

【问题讨论】:

  • 不知道为什么会得到 404。应该没有猜测的余地。您可以在开发工具中清楚地看到请求,检查网络选项卡。他们要么有正确的网址,要么没有。如果是,则问题出在后端,否则出在前端。
  • 你是对的 Estus,它可能是后端,我原以为它可能来自端点。我该如何尝试?
  • 对于初学者,您需要检查开发工具中的网络选项卡,如上所述。那么问题可以被隔离到前端或后端
  • 我已经编辑了我的问题以包含一对屏幕截图,你能给我确定我是否需要隔离一个或另一个的关键吗?谢谢
  • 未知正确的 url 应该是什么。不知道是什么反应。可能是 404 由不正确的 url 引起,也可能是 api 响应,因为您尝试以不存在的用户身份登录 - 很可能是第一个,但无论如何。

标签: vue.js routes web-hosting asp.net-core-3.1


【解决方案1】:

如果您在本地或托管,请提供baseURL,并采用 router.js 中的每条路线,然后选择您要访问的路径页面,然后 这么写

baseURL/你的页面路径

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 2021-03-03
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    • 2015-03-10
    相关资源
    最近更新 更多