gpt4 book ai didi

asp.net-core - CSHTML View 在应用程序重新启动之前不会更新

转载 作者:行者123 更新时间:2023-12-05 02:16:36 24 4
gpt4 key购买 nike

我正在使用 browserlink 来编辑 CSS 样式,效果非常好。不幸的是,如果我更改 .cshtml 文件中的某些内容,我的浏览器会在保存时自动刷新,但更改是不可见的。

如果我关闭我的应用程序并再次打开,更改是可见的。似乎我的应用程序以某种方式在某处缓存 View ,而不是将我所做的更改重新加载到我的文件中。

这真的不是浏览器缓存的问题。应用程序真正发送未更改的 html 结果。

如何在开发中禁用此类缓存功能?

我正在使用最新的 ASP NET Core MVC 库。

编辑:如果我更改 _layout 中的任何内容,网站会毫无问题地更新。

编辑 2:启动函数

        public void ConfigureServices(IServiceCollection services)
{
services.AddWebSocketManager();

var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var path = System.AppDomain.CurrentDomain.BaseDirectory;

var machineName = Environment.MachineName;
var confBuilder = new ConfigurationBuilder();
IConfigurationBuilder conf = confBuilder.SetBasePath(path);

if (env == "Development")
{
conf = conf.AddJsonFile($"appsettings.json", optional: true, reloadOnChange: true);
conf = conf.AddJsonFile($"appsettings.Development.json", optional: true, reloadOnChange: true);
conf = conf.AddJsonFile($"appsettings.{machineName}.json", optional: true, reloadOnChange: true);
conf = conf.AddJsonFile($"appsettings.External.json", optional: true, reloadOnChange: true);
}
else
{
conf = conf.AddJsonFile($"appsettings.json", optional: true, reloadOnChange: true);
conf = conf.AddJsonFile($"appsettings.Production.json", optional: true, reloadOnChange: true);
conf = conf.AddJsonFile($"appsettings.External.json", optional: true, reloadOnChange: true);
}
Configuration = conf.Build();
services.AddSingleton(provider => Configuration);

CoreStarter.OnServiceConfiguration?.Invoke(Configuration, services);

var settings = new JsonSerializerSettings();
settings.ContractResolver = new SignalRContractResolver();
var serializer = JsonSerializer.Create(settings);
services.Add(new ServiceDescriptor(typeof(JsonSerializer),provider => serializer,ServiceLifetime.Transient));

services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.TryAddSingleton<IContextService, ContextService>();
services.TryAddSingleton<ICryptoService, CryptoService>();
services.TryAddSingleton<IAuthorizationHandler, AuthenticationHandler>();
services.TryAddSingleton<IHttpService, RestApiService>();

if (services.Any(x => x.ServiceType == typeof(IIdentityService)))
{
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
{
options.Events.OnRedirectToLogin = (context) =>
{
context.Response.StatusCode = 401;
return Task.CompletedTask;
};
options.Events.OnRedirectToAccessDenied = (context) =>
{
context.Response.StatusCode = 401;
return Task.CompletedTask;
};

var sharedCookiePath = Configuration.GetJsonKey<string>("SharedCookiePath");
if (!String.IsNullOrWhiteSpace(sharedCookiePath))
{
options.DataProtectionProvider = DataProtectionProvider.Create(new DirectoryInfo(sharedCookiePath));
}
});
}


services.AddCors();
services.AddLogging(builder =>
{
builder.AddConsole().AddDebug();
});
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info {Title = "CoreR API", Version = "v1"});
});

services.AddDistributedMemoryCache();
services.AddSession();
services.AddMemoryCache();
services.AddSingleton<IAssemblyLocator, BaseAssemblyLocator>();
services.AddSignalR(options =>
{
options.Hubs.EnableDetailedErrors = true;
});

var mvcBuilder = services.AddMvc(config =>
{
if (services.Any(x => x.ServiceType == typeof(IIdentityService)))
{
var policyBuilder = new AuthorizationPolicyBuilder();
policyBuilder.RequireAuthenticatedUser();
policyBuilder.AddRequirements(new AuthenticationRequirement());

var policy = policyBuilder.Build();
config.Filters.Add(new AuthorizeFilter(policy));
}
})
.AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});

var assemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();

foreach (var assembly in assemblies)
{
mvcBuilder.AddApplicationPart(assembly);
}

ServiceProvider = services.BuildServiceProvider();
}

public void Configure(IApplicationBuilder app, IServiceProvider serviceProvider, IHostingEnvironment env)
{
var embeddedProvider = new EmbeddedFileProvider(Assembly.GetExecutingAssembly());
var physicalProvider = env.ContentRootFileProvider;
var compositeProvider = new CompositeFileProvider(physicalProvider, embeddedProvider);


app.UseCors(o => o.AllowAnyOrigin().AllowCredentials().AllowAnyMethod().AllowAnyHeader());
app.UseAuthentication();

app.UseBrowserLink();
app.UseDeveloperExceptionPage();

app.UseDefaultFiles(new DefaultFilesOptions()
{
FileProvider = compositeProvider,
DefaultFileNames = new List<string>() { "default.html"},
});
app.UseStaticFiles(new StaticFileOptions {FileProvider = compositeProvider});
app.UseSession();
app.UseWebSockets();
app.UseSignalR();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "CoreR API");
});

app.UseMvc(routes =>
{
routes.MapRoute("Default", "api/{controller}/{action}/{id?}");
});

CoreStarter.OnConfiguration?.Invoke(Configuration, app, serviceProvider, env);
}

最佳答案

根据 Razor file compilation in ASP.NET Core如果您只想为本地开发启用运行时编译:

1.根据事件配置值有条件地引用Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation包:

<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.0" Condition="'$(Configuration)' == 'Debug'" />

2。更新项目的Startup.cs文件ConfigureServices方法:

public void ConfigureServices(IServiceCollection services)
{
IMvcBuilder builder = services.AddRazorPages();

if (env.IsDevelopment())
{
builder.AddRazorRuntimeCompilation();
}
}

关于asp.net-core - CSHTML View 在应用程序重新启动之前不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49403409/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com