【问题标题】:Static files works only on default endpoint ASP NET CORE MVC静态文件仅适用于默认端点 ASP NET CORE MVC
【发布时间】:2021-07-19 23:01:00
【问题描述】:

问题是静态文件仅在默认端点上加载。 如果我手动输入端点路由静态文件将不会加载 我将 ~ 字符添加到每个脚本标签,但它不起作用 Same Page but on endpoint

When Type same controller and action on URL

Startup.cs

    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews().AddRazorRuntimeCompilation();
            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy",
                    builder => builder
                        .AllowAnyMethod()
                        .AllowCredentials()
                        .SetIsOriginAllowed((host) => true)
                        .AllowAnyHeader());
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseDefaultFiles();
            app.UseStaticFiles();
            app.UseHttpsRedirection();
            app.UseRouting();
 
            app.UseAuthorization();
            app.UseCors("CorsPolicy");

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Customer}/{action=Customers}/{id?}");
            });
        }
    }
}```



Thank you for your advice

【问题讨论】:

  • 展示你使用静态文件的例子。
  • 脚本部分
  • 不要添加到 cmets,edit 你的问题的细节。还要确保你解释了这些文件在你的结构中的位置。
  • 嗨@FarHard112,它在我的项目中运行良好,尽管我更改了不同控制器和操作的url。您在哪里添加了用于渲染此类图像的代码,您能否分享代码?你能分享一下你的静态文件的项目结构吗?
  • @**@

    用户名

标签: c# asp.net-core asp.net-core-mvc


【解决方案1】:

问题是:添加路径时我忘记添加一些文件 ~(tilda)
这很愚蠢,但它是真实的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 2021-09-29
    • 1970-01-01
    • 2021-07-20
    • 2021-06-07
    • 1970-01-01
    相关资源
    最近更新 更多