我正在使用browserlink来编辑CSS样式,它的工作非常好。不幸的是,如果我在.cshtml文件中更改某些内容,我的浏览器会在保存时自动刷新,但更改不可见。
如果我关闭我的应用程序并再次打开,则可以看到更改。 看起来我的应用程序在某处以某种方式缓存视图,而不是重新加载我在文件中所做的更改。
这实际上不是浏览器缓存问题。应用程序确实发送了未更改的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);
}
答案 0 :(得分:3)
根据Razor file compilation in ASP.NET Core,如果您只想为本地开发启用运行时编译:
1。根据活动的Configuration值有条件地引用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();
}
}