有没有一种方法可以通过MVC应用程序中的特定控制器操作来提供Blazor应用程序? [英] Is there a way to serve a Blazor app from a specific controller action in a MVC app?

查看:56
本文介绍了有没有一种方法可以通过MVC应用程序中的特定控制器操作来提供Blazor应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个现有的ASP.NET Core 3.0 MVC项目中设置一个控制器动作,以服务Blazor应用程序.我过去曾见过与其他SPA框架类似的内容,操作视图"仅包含包含捆绑包的script标签,通常还有其他一些设置,例如将SPA构建放在MVC项目的wwwroot上.

I'd like to set up a controller action in a already existing ASP.NET Core 3.0 MVC project to serve a Blazor app. I've seen something similar with other SPA frameworks in the past, the View for the action just includes contains the script tag with the bundle, there's usually some other setup like having the SPA build placed on wwwroot for the MVC project.

在Blazor应用程序看来,index.html文件包含wasm脚本在某种程度上相似.但老实说,我不确定从哪里开始进行设置.有什么想法吗?

Looking at the Blazor app it seems similar to some extent, where the index.html file includes the wasm script. But I'm honestly not sure where to start to set it up. Any ideas?

推荐答案

似乎您正在使用Blazor客户端.基本上,Blazor客户端应用程序的条目加载 blazor.webassembly.js < script> >.并且此脚本将动态下载所有静态资产( *.wasm *.dlls ).请注意,如果可以使这些资产可用,即使没有ASP.NET Core,也可以托管Blazor客户端应用程序.

Seems you're using Blazor Client Side. Basically, the entry for Blazor Client Side App is the <script> that loads the blazor.webassembly.js. And this script will download all the static assets( *.wasm and *.dlls) dynamically. Note that if you can make these assets available, you can host the Blazor Client Side App even without ASP.NET Core.

由于您使用ASP.NET Core MVC作为服务器,因此一种简单的方法是:

Since you're using ASP.NET Core MVC as the server, an easy approach would be :

  1. 添加对 Microsoft.AspNetCore.Blazor.Server 的程序包引用,以便我们可以为 *.wasm *.dll 文件提供服务只需一个代码.
  1. Add a package reference to Microsoft.AspNetCore.Blazor.Server so that we can serve the *.wasm *.dll files with just one code.
<PackageReference Include="Microsoft.AspNetCore.Blazor.Server" Version="3.0.0-preview9.19465.2" />

  • 向您的ClientSide项目添加项目引用,以便我们可以从源代码生成静态信息.假设您的客户端Web组装应用程序是 MyClientSideBlazorApp :

    <ProjectReference Include="..\MyClientSideBlazorApp\MyClientSideBlazorApp.csproj" />
    

  • 现在您可以在Startup.cs中注册一个中间件来提供静态文件:

  • And now you can register a middleware within the Startup.cs to serve the static files:

    app.UseClientSideBlazorFiles<MyClientSideBlazorApp.Startup>();
    app.UseStaticFiles();
    

  • 最后,在要为其提供服务的任何页面/视图中,例如 Home/Privacy.cshtml 视图文件,添加< script> 来加载条目,还加载一个< base> 元素以确保相对路径正确.

  • Finally, within any page/view that you want to serve the blazor, for example, the Home/Privacy.cshtml view file, add a <script> to load the entry and also a <base> element to make sure the relative path is correct.

    @{
       Layout = null;
    }
    <base href="/">
    
    ... 
    
    <app>Loading...</app>
    
    <script src="/_framework/blazor.webassembly.js"></script>
    

  • 现在应该可以正常工作了.

    It should work fine now.

    这篇关于有没有一种方法可以通过MVC应用程序中的特定控制器操作来提供Blazor应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆