添加 Razor 页面组件到 MVC

huangapple go评论105阅读模式
英文:

Add Razor Page Component in MVC

问题

I've added Razor Page Component to my existing MVC application

Pages/Test.razor

  1. @page "/test";
  2. <h1>Hello World!</h1>
  3. @code {
  4. }

Program.cs

  1. var builder = WebApplication.CreateBuilder(args);
  2. // Add services to the container.
  3. builder.Services.AddControllersWithViews();
  4. builder.Services.AddServerSideBlazor();
  5. builder.Services.AddRazorPages();
  6. var app = builder.Build();
  7. // Configure the HTTP request pipeline.
  8. if (!app.Environment.IsDevelopment())
  9. {
  10. app.UseExceptionHandler("/Home/Error");
  11. }
  12. app.UseStaticFiles();
  13. app.UseRouting();
  14. app.UseAuthorization();
  15. app.UseEndpoints(endpoints =>
  16. {
  17. endpoints.MapControllerRoute(
  18. name: "default",
  19. pattern: "{controller=Home}/{action=Index}/{id?}");
  20. endpoints.MapRazorPages();
  21. endpoints.MapBlazorHub();
  22. });
  23. app.Run();

Views/Shared/_Layout.cshtml

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>@ViewData["Title"] - MVCApp</title>
  7. <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
  8. <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
  9. <link rel="stylesheet" href="~/MVCApp.styles.css" asp-append-version="true" />
  10. <base href="/" />
  11. <script src="_framework/blazor.server.js"></script>
  12. ...

但它不起作用:在/test上我得到404错误。

你需要做什么来使Razor Component的路由与MVC路由一起工作呢?

英文:

I've added Razor Page Component to my existing MVC application

Pages/Test.razor

  1. @page &quot;/test&quot;
  2. &lt;h1&gt;Hello World!&lt;/h1&gt;
  3. @code {
  4. }

Program.cs

  1. var builder = WebApplication.CreateBuilder(args);
  2. // Add services to the container.
  3. builder.Services.AddControllersWithViews();
  4. builder.Services.AddServerSideBlazor();
  5. builder.Services.AddRazorPages();
  6. var app = builder.Build();
  7. // Configure the HTTP request pipeline.
  8. if (!app.Environment.IsDevelopment())
  9. {
  10. app.UseExceptionHandler(&quot;/Home/Error&quot;);
  11. }
  12. app.UseStaticFiles();
  13. app.UseRouting();
  14. app.UseAuthorization();
  15. app.UseEndpoints(endpoints =&gt;
  16. {
  17. endpoints.MapControllerRoute(
  18. name: &quot;default&quot;,
  19. pattern: &quot;{controller=Home}/{action=Index}/{id?}&quot;);
  20. endpoints.MapRazorPages();
  21. endpoints.MapBlazorHub();
  22. });
  23. app.Run();

Views/Shared/_Layout.cshtml

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;en&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;utf-8&quot; /&gt;
  5. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
  6. &lt;title&gt;@ViewData[&quot;Title&quot;] - MVCApp&lt;/title&gt;
  7. &lt;link rel=&quot;stylesheet&quot; href=&quot;~/lib/bootstrap/dist/css/bootstrap.min.css&quot; /&gt;
  8. &lt;link rel=&quot;stylesheet&quot; href=&quot;~/css/site.css&quot; asp-append-version=&quot;true&quot; /&gt;
  9. &lt;link rel=&quot;stylesheet&quot; href=&quot;~/MVCApp.styles.css&quot; asp-append-version=&quot;true&quot; /&gt;
  10. &lt;base href=&quot;/&quot;/&gt;
  11. &lt;script src=&quot;_framework/blazor.server.js&quot;&gt;&lt;/script&gt;
  12. ...

but it doesn't work: I get 404 on /test

添加 Razor 页面组件到 MVC

What am I missing to make Razor Component's routing work with MVC routing?

答案1

得分: 0

你需要安装 Microsoft.AspNetCore.Components 包,并将 _Host.cshtmlApp.razor_Imports.razor 文件添加到你的 MVC 项目中。

_Imports.razor:

  1. @using System.Net.Http
  2. @using Microsoft.AspNetCore.Authorization
  3. @using Microsoft.AspNetCore.Components.Authorization
  4. @using Microsoft.AspNetCore.Components.Forms
  5. @using Microsoft.AspNetCore.Components.Routing
  6. @using Microsoft.AspNetCore.Components.Web
  7. @using Microsoft.AspNetCore.Components.Web.Virtualization
  8. @using Microsoft.JSInterop
  9. @using Project

_Host.cshtml:

  1. @page &quot;/&quot;
  2. @namespace Project.Pages
  3. @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
  4. @{
  5. Layout = &quot;_Layout&quot;;
  6. }
  7. &lt;component type=&quot;typeof(App)&quot; render-mode=&quot;ServerPrerendered&quot; /&gt;

App.razor:

  1. @using Microsoft.AspNetCore.Components.Routing
  2. &lt;Router AppAssembly=&quot;@typeof(App).Assembly&quot;&gt;
  3. &lt;Found Context=&quot;routeData&quot;&gt;
  4. &lt;RouteView RouteData=&quot;@routeData&quot; /&gt;
  5. &lt;FocusOnNavigate RouteData=&quot;@routeData&quot; Selector=&quot;h1&quot; /&gt;
  6. &lt;/Found&gt;
  7. &lt;NotFound&gt;
  8. &lt;PageTitle&gt;Not found&lt;/PageTitle&gt;
  9. &lt;LayoutView&gt;
  10. &lt;p role=&quot;alert&quot;&gt;Sorry, there&#39;s nothing at this address.&lt;/p&gt;
  11. &lt;/LayoutView&gt;
  12. &lt;/NotFound&gt;
  13. &lt;/Router&gt;

你的端点:

  1. app.UseEndpoints(endpoints =&gt;
  2. {
  3. endpoints.MapControllerRoute(
  4. name: &quot;default&quot;,
  5. pattern: &quot;{controller=Home}/{action=Index}/{id?}&quot;);
  6. endpoints.MapRazorPages();
  7. endpoints.MapBlazorHub();
  8. endpoints.MapFallbackToPage(&quot;/_Host&quot;);
  9. });

测试结果:

添加 Razor 页面组件到 MVC

如果你想在 Blazor 中使用 UI,你也可以从 Blazor 应用程序复制 MainLayout.razor 并在 App.razor 中使用它,就像这样:

  1. //...
  2. &lt;RouteView RouteData=&quot;@routeData&quot; DefaultLayout=&quot;@typeof(MainLayout)&quot; /&gt;
  3. //...
  4. &lt;LayoutView Layout=&quot;@typeof(MainLayout)&quot;&gt;
  5. //...

请注意,如果你这样做,你的默认页面将是一个 Razor 组件,其中定义了 @page &quot;/&quot;,而不是 Index.cshtml

英文:

You need to install the Microsoft.AspNetCore.Components package and add the _Host.cshtml, App.razor, _Imports.razor files to your MVC project.

_Imports.razor:

  1. @using System.Net.Http
  2. @using Microsoft.AspNetCore.Authorization
  3. @using Microsoft.AspNetCore.Components.Authorization
  4. @using Microsoft.AspNetCore.Components.Forms
  5. @using Microsoft.AspNetCore.Components.Routing
  6. @using Microsoft.AspNetCore.Components.Web
  7. @using Microsoft.AspNetCore.Components.Web.Virtualization
  8. @using Microsoft.JSInterop
  9. @using Project

_Host.cshtml:

  1. @page &quot;/&quot;
  2. @namespace Project.Pages
  3. @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
  4. @{
  5. Layout = &quot;_Layout&quot;;
  6. }
  7. &lt;component type=&quot;typeof(App)&quot; render-mode=&quot;ServerPrerendered&quot; /&gt;

App.razor:

  1. @using Microsoft.AspNetCore.Components.Routing
  2. &lt;Router AppAssembly=&quot;@typeof(App).Assembly&quot;&gt;
  3. &lt;Found Context=&quot;routeData&quot;&gt;
  4. &lt;RouteView RouteData=&quot;@routeData&quot; /&gt;
  5. &lt;FocusOnNavigate RouteData=&quot;@routeData&quot; Selector=&quot;h1&quot; /&gt;
  6. &lt;/Found&gt;
  7. &lt;NotFound&gt;
  8. &lt;PageTitle&gt;Not found&lt;/PageTitle&gt;
  9. &lt;LayoutView&gt;
  10. &lt;p role=&quot;alert&quot;&gt;Sorry, there&#39;s nothing at this address.&lt;/p&gt;
  11. &lt;/LayoutView&gt;
  12. &lt;/NotFound&gt;
  13. &lt;/Router&gt;

Your Endpoints:

  1. app.UseEndpoints(endpoints =&gt;
  2. {
  3. endpoints.MapControllerRoute(
  4. name: &quot;default&quot;,
  5. pattern: &quot;{controller=Home}/{action=Index}/{id?}&quot;);
  6. endpoints.MapRazorPages();
  7. endpoints.MapBlazorHub();
  8. endpoints.MapFallbackToPage(&quot;/_Host&quot;);
  9. });

Test Result:

添加 Razor 页面组件到 MVC

If you want to use the UI in Blazor, you can also copy MainLayout.razor from a Blazor App and use it in App.razor like this:

  1. //...
  2. &lt;RouteView RouteData=&quot;@routeData&quot; DefaultLayout=&quot;@typeof(MainLayout)&quot; /&gt;
  3. //...
  4. &lt;LayoutView Layout=&quot;@typeof(MainLayout)&quot;&gt;
  5. //...

Note that if you do this, your default page will be a Razor Component with @page &quot;/&quot; defined instead of Index.cshtml.

huangapple
  • 本文由 发表于 2023年2月18日 23:29:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定