CSHTML 视图在应用程序重新启动之前不会更新
Posted
技术标签:
【中文标题】CSHTML 视图在应用程序重新启动之前不会更新【英文标题】:CSHTML Views are not updated until application restart 【发布时间】:2018-03-21 10:07:54 【问题描述】:我正在使用 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);
【问题讨论】:
开箱即用不会发生缓存。但是,如果您添加了诸如响应缓存中间件之类的东西,那么这可能是个问题。向我们展示您的 Startup.cs。 我加了一些启动功能,有点大呵呵 我看到的唯一可能存在问题的是UseDefaultFiles
,它使用静态文件中间件来实际提供静态 HTML 文件。如果它实际上是 default.html
没有刷新,它可能被浏览器缓存。 (如果浏览器直接使用缓存,看起来服务器正在发送相同的响应)。
所有 cshtml 文件在应用重启之前不会刷新。如果我更改 layout.cshtml。它的刷新很好
这是同一个问题吗? ***.com/questions/53639969/…
【参考方案1】:
根据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();
【讨论】:
我不知道您可以有条件地添加 NuGet 包!不过,从 ASP.NET Core 3.1 开始,这可以通过编辑launchSettings.json
文件而不是 Startup.cs
文件来完成:docs.microsoft.com/en-us/aspnet/core/mvc/views/…以上是关于CSHTML 视图在应用程序重新启动之前不会更新的主要内容,如果未能解决你的问题,请参考以下文章
除非重新启动项目,否则 VS .Net Core Razor 页面不会更新
Enmap (better-sqlite map) 在应用程序重新启动之前不会更新
iOS 为啥我的应用程序在重新启动之前不会将数据加载到 tableview 中?
Core Data 应用程序中的 UITableView 不会更新第二个设备的主视图,除非重新启动应用程序,并使用 iCloud 同步