重新保存Blazor WASM的.razor文件 [英] Recompile .razor files on save for Blazor WASM
问题描述
是否有一种方法可以使Blazor Webassembly在更改/更新然后保存后重新编译 .razor
文件?我已经习惯了在传统的ASP.NET Core MVC剃刀视图以及客户端框架(例如Angular)中发生这种情况.在ASP.NET Core MVC> 3.0中,类似 services.AddRazorPages().AddRazorRuntimeCompilation();
可以解决问题,但Blazor却找不到我能找到的东西.
Is there a way to make Blazor Webassembly recompile .razor
files when they're changed/updated and then saved? I'm used to this happening both in traditional ASP.NET Core MVC razor views as well as client-side frameworks like Angular.
In ASP.NET Core MVC >3.0, something like
services.AddRazorPages().AddRazorRuntimeCompilation();
would do the trick, but nothing exists for Blazor that I could find.
当我需要停止整个应用程序并重新启动以查看最新更改时,这很烦人.默认情况下,用于新Blazor应用程序的 Main
方法看起来像
It's annoying when I need to stop the entire application and restart it to see the latest changes. By default the Main
method for a new Blazor application looks like
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddTransient(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
await builder.Build().RunAsync();
}
我在这里想念什么?Blazor WASM还在吗?如果这是一种解决方案,那么我很乐意接受像 dotnet watch
dotnet build
这样的小技巧.
What am I missing here? Is Blazor WASM just not there yet? I'm open to something a little hacky like dotnet watch
dotnet build
if that's a solution.
推荐答案
从.NET 5.0和VS 16.8.1开始,它似乎没有内置的HMR,就像您在Angular或React项目中会发现的那样,您可以在其中更改代码并使它刷新浏览器.
As of .NET 5.0 and VS 16.8.1 it doesn't look like there is a built in HMR like you would find with an Angular or React project, where you can change code and have it refresh the browser.
根据我发现的信息,此功能适用于.NET 6,但是有解决方法可以使此功能正常工作,虽然有点麻烦,但确实可以工作.
According to the info I have found, this functionality is slated for .NET 6 but there is workaround to get this working, it is a little cumbersome but it does work.
如果您使用 dotnet watch -project"{PathToServer}/{YourServerProject} .csproj"运行.NET项目,运行
,而不是开始调试或按F5键,它将监视项目文件中的所有更改,包括 .razor
文件,然后重新编译它们.
If you run the .NET project using dotnet watch --project "{PathToServer}/{YourServerProject}.csproj" run
instead of starting the debug or pressing F5 it will watch for any changes in the project's files including the .razor
files and recompile them.
截至目前,浏览器将显示一条消息,指出它与服务器的连接已断开并且似乎未重新建立连接.您需要手动刷新浏览器,并且所有更改都会显示,但是您将丢失任何应用程序状态.
As of right now the browser will display a message saying that it lost connection to the server and doesn't seem to reestablish a connection. You are required to refresh the browser manually and any changes will be displayed but you will lose any application state.
最好,他们现在有查看此主题以获取更多信息
这篇关于重新保存Blazor WASM的.razor文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!