【问题标题】:Loading Static Files from Controller ASP Net Core MVC从控制器 ASP Net Core MVC 加载静态文件
【发布时间】:2021-07-23 01:13:34
【问题描述】:

我有一个令人费解的问题,我的应用程序从控制器文件夹加载静态文件,但在默认路由中它正常加载

例子:

https://localhost:44369/plugins/bootstrap/js/bootstrap.bundle.min.js (in normal situation) (needs to be work)

我的情况

https://localhost:44369/Order/plugins/bootstrap/js/bootstrap.bundle.min.js

它想从不存在的控制器文件夹中加载静态文件。

我使用的是 Admin LTE 3 模板(可能模板有问题?)

谢谢你:)

Startup.cs

public class Startup
{
    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.AddHttpClient();
        services.AddAutoMapper(typeof(Startup));
        services.AddControllersWithViews().AddRazorRuntimeCompilation();
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        services.AddSingleton<ICustomerService, CustomerManager>();
        services.AddSingleton<ICustomerDal, CustomerDal>();
        services.AddSingleton<IInformationResourcesService, InformationResourcesManager>();
        services.AddSingleton<IInformationResources, EFInformationResourcesDAL>();
        services.AddSingleton<INewOrderService, NewOrderManager>();
        services.AddSingleton<INewOrderService, NewOrderManager>();
        services.AddSingleton<INewOrderDal, EfNewOrderDalL>();
        services.AddSingleton<ICityDal, EFCityDal>();
        services.AddSingleton<ICityService, CityManager>();
        services.AddSingleton<IDisctrictDal, EFDistrictDal>();
        services.AddSingleton<IDistrictService, DistrictManager>();
        services.AddSingleton<IVillageDal, EFVillageDal>();
        services.AddSingleton<IVillageService, VillageManager>();
        services.AddSingleton<IStreetDal, EFStreetDal>();
        services.AddSingleton<IStreetService, StreetManager>();
        services.AddSingleton<ICustomerAdressService, CustomerAdressManager>();
        services.AddSingleton<ICustomerAdressDal, EFCustomerAdressDal>();

        //services.AddCors(options =>
        //{
        //    options.AddPolicy("CorsPolicy",
        //        builder => builder
        //            .AllowAnyMethod()
        //            .AllowCredentials()
        //            .SetIsOriginAllowed((host) => true)
        //            .AllowAnyHeader());
        //});
        //services.AddDependencyResolvers(new ICoreModule[] {
        //    new CoreModule()
        //});
    }

    // 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.UseStaticFiles();
        app.UseHttpsRedirection();
        app.UseRouting();

        app.UseAuthorization();
        //app.UseCors("CorsPolicy");

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

【问题讨论】:

  • 您好,欢迎来到 Stack Overflow。请阅读指南并改写问题:stackoverflow.com/help/how-to-ask
  • 您的意思是要将css或js文件加载到控制器文件夹而不是从wwwroot的文件夹中?

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


【解决方案1】:

您能否指定如何将脚本添加到您的页面。 我也在使用模板,我的脚本位于 wwwroot/vendors 文件夹中 这是我的脚本标签。

<script src="~/vendors/jquery/dist/jquery.min.js" type="text/javascript"></script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-27
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多