在 ASP.NET Core Blazor 專案編譯後的 .cshtml & .razor 變成甚麼呢?
@page "/counter"
<h1>Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.AspNetCore.Components.Web;
using System;
[Route("/counter")]
public class Counter : ComponentBase
{
private int currentCount = 0;
protected override void BuildRenderTree(RenderTreeBuilder __builder)
{
__builder.AddMarkupContent(0, "<h1>Counter</h1>\r\n\r\n");
__builder.OpenElement(1, "p");
__builder.AddContent(2, "Current count: ");
__builder.AddContent(3, currentCount);
__builder.CloseElement();
__builder.AddMarkupContent(4, "\r\n\r\n");
__builder.OpenElement(5, "button");
__builder.AddAttribute(6, "class", "btn btn-primary");
__builder.AddAttribute(7, "onclick", EventCallback.Factory.Create<MouseEventArgs>((object)this, (Action)IncrementCount));
__builder.AddContent(8, "Click me");
__builder.CloseElement();
}
private void IncrementCount()
{
currentCount++;
}
}
@page "/"
@namespace BlazorApp10.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>BlazorApp10</title>
<base href="~/" />
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
<link href="css/site.css" rel="stylesheet" />
</head>
<body>
<app>
<component type="typeof(App)" render-mode="ServerPrerendered" />
</app>
<div id="blazor-error-ui">
<environment include="Staging,Production">
An error has occurred. This application may no longer respond until reloaded.
</environment>
<environment include="Development">
An unhandled exception has occurred. See browser dev tools for details.
</environment>
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>
<script src="_framework/blazor.server.js"></script>
</body>
</html>
using BlazorApp10;
using BlazorApp10.Pages;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.AspNetCore.Mvc.Razor.TagHelpers;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.Hosting;
using Microsoft.AspNetCore.Razor.Runtime.TagHelpers;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
[RazorCompiledItemMetadata("RouteTemplate", "/")]
[RazorSourceChecksum("SHA1", "c4eef6e21b532050217466a050cf103768022b7b", "/Pages/_Host.cshtml")]
public class Pages__Host : Page
{
private static readonly TagHelperAttribute __tagHelperAttribute_0 = (TagHelperAttribute)(object)new TagHelperAttribute("href", (object)new HtmlString("~/"), (HtmlAttributeValueStyle)0);
private static readonly TagHelperAttribute __tagHelperAttribute_1 = (TagHelperAttribute)(object)new TagHelperAttribute("include", (object)"Staging,Production", (HtmlAttributeValueStyle)0);
private static readonly TagHelperAttribute __tagHelperAttribute_2 = (TagHelperAttribute)(object)new TagHelperAttribute("include", (object)"Development", (HtmlAttributeValueStyle)0);
private TagHelperExecutionContext __tagHelperExecutionContext;
private TagHelperRunner __tagHelperRunner = (TagHelperRunner)(object)new TagHelperRunner();
private string __tagHelperStringValueBuffer;
private TagHelperScopeManager __backed__tagHelperScopeManager = null;
private HeadTagHelper __Microsoft_AspNetCore_Mvc_Razor_TagHelpers_HeadTagHelper;
private UrlResolutionTagHelper __Microsoft_AspNetCore_Mvc_Razor_TagHelpers_UrlResolutionTagHelper;
private BodyTagHelper __Microsoft_AspNetCore_Mvc_Razor_TagHelpers_BodyTagHelper;
private ComponentTagHelper __Microsoft_AspNetCore_Mvc_TagHelpers_ComponentTagHelper;
private EnvironmentTagHelper __Microsoft_AspNetCore_Mvc_TagHelpers_EnvironmentTagHelper;
private TagHelperScopeManager __tagHelperScopeManager
{
get
{
if (__backed__tagHelperScopeManager == null)
{
__backed__tagHelperScopeManager = (TagHelperScopeManager)(object)new TagHelperScopeManager((Action<HtmlEncoder>)base.StartTagHelperWritingScope, (Func<TagHelperContent>)base.EndTagHelperWritingScope);
}
return __backed__tagHelperScopeManager;
}
}
[RazorInject]
public IModelExpressionProvider ModelExpressionProvider
{
get;
private set;
}
[RazorInject]
public IUrlHelper Url
{
get;
private set;
}
[RazorInject]
public IViewComponentHelper Component
{
get;
private set;
}
[RazorInject]
public IJsonHelper Json
{
get;
private set;
}
[RazorInject]
public IHtmlHelper<Pages__Host> Html
{
get;
private set;
}
public ViewDataDictionary<Pages__Host> ViewData => (ViewDataDictionary<Pages__Host>)(object)base.PageContext?.ViewData;
public Pages__Host Model => ViewData.get_Model();
public override async Task ExecuteAsync()
{
((RazorPageBase)this).set_Layout((string)null);
((RazorPageBase)this).WriteLiteral("\r\n<!DOCTYPE html>\r\n<html lang=\"en\">\r\n");
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("head", (TagMode)0, "c4eef6e21b532050217466a050cf103768022b7b4361", (Func<Task>)async delegate
{
((RazorPageBase)this).WriteLiteral("\r\n <meta charset=\"utf-8\" />\r\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\r\n <title>BlazorApp10</title>\r\n ");
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("base", (TagMode)1, "c4eef6e21b532050217466a050cf103768022b7b4775", (Func<Task>)async delegate
{
});
__Microsoft_AspNetCore_Mvc_Razor_TagHelpers_UrlResolutionTagHelper = ((RazorPageBase)this).CreateTagHelper<UrlResolutionTagHelper>();
__tagHelperExecutionContext.Add((ITagHelper)(object)__Microsoft_AspNetCore_Mvc_Razor_TagHelpers_UrlResolutionTagHelper);
__tagHelperExecutionContext.AddHtmlAttribute(__tagHelperAttribute_0);
await __tagHelperRunner.RunAsync(__tagHelperExecutionContext);
if (!__tagHelperExecutionContext.get_Output().get_IsContentModified())
{
await __tagHelperExecutionContext.SetOutputContentAsync();
}
((RazorPageBase)this).Write((object)__tagHelperExecutionContext.get_Output());
__tagHelperExecutionContext = __tagHelperScopeManager.End();
((RazorPageBase)this).WriteLiteral("\r\n <link rel=\"stylesheet\" href=\"css/bootstrap/bootstrap.min.css\" />\r\n <link href=\"css/site.css\" rel=\"stylesheet\" />\r\n");
});
__Microsoft_AspNetCore_Mvc_Razor_TagHelpers_HeadTagHelper = ((RazorPageBase)this).CreateTagHelper<HeadTagHelper>();
__tagHelperExecutionContext.Add((ITagHelper)(object)__Microsoft_AspNetCore_Mvc_Razor_TagHelpers_HeadTagHelper);
await __tagHelperRunner.RunAsync(__tagHelperExecutionContext);
if (!__tagHelperExecutionContext.get_Output().get_IsContentModified())
{
await __tagHelperExecutionContext.SetOutputContentAsync();
}
((RazorPageBase)this).Write((object)__tagHelperExecutionContext.get_Output());
__tagHelperExecutionContext = __tagHelperScopeManager.End();
((RazorPageBase)this).WriteLiteral("\r\n");
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("body", (TagMode)0, "c4eef6e21b532050217466a050cf103768022b7b6703", (Func<Task>)async delegate
{
((RazorPageBase)this).WriteLiteral("\r\n <app>\r\n ");
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("component", (TagMode)1, "c4eef6e21b532050217466a050cf103768022b7b6982", (Func<Task>)async delegate
{
});
__Microsoft_AspNetCore_Mvc_TagHelpers_ComponentTagHelper = ((RazorPageBase)this).CreateTagHelper<ComponentTagHelper>();
__tagHelperExecutionContext.Add((ITagHelper)(object)__Microsoft_AspNetCore_Mvc_TagHelpers_ComponentTagHelper);
__Microsoft_AspNetCore_Mvc_TagHelpers_ComponentTagHelper.set_ComponentType(typeof(App));
__tagHelperExecutionContext.AddTagHelperAttribute("type", (object)__Microsoft_AspNetCore_Mvc_TagHelpers_ComponentTagHelper.get_ComponentType(), (HtmlAttributeValueStyle)0);
__Microsoft_AspNetCore_Mvc_TagHelpers_ComponentTagHelper.set_RenderMode((RenderMode)3);
__tagHelperExecutionContext.AddTagHelperAttribute("render-mode", (object)__Microsoft_AspNetCore_Mvc_TagHelpers_ComponentTagHelper.get_RenderMode(), (HtmlAttributeValueStyle)0);
await __tagHelperRunner.RunAsync(__tagHelperExecutionContext);
if (!__tagHelperExecutionContext.get_Output().get_IsContentModified())
{
await __tagHelperExecutionContext.SetOutputContentAsync();
}
((RazorPageBase)this).Write((object)__tagHelperExecutionContext.get_Output());
__tagHelperExecutionContext = __tagHelperScopeManager.End();
((RazorPageBase)this).WriteLiteral("\r\n </app>\r\n\r\n <div id=\"blazor-error-ui\">\r\n ");
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("environment", (TagMode)0, "c4eef6e21b532050217466a050cf103768022b7b9004", (Func<Task>)async delegate
{
((RazorPageBase)this).WriteLiteral("\r\n An error has occurred. This application may no longer respond until reloaded.\r\n ");
});
__Microsoft_AspNetCore_Mvc_TagHelpers_EnvironmentTagHelper = ((RazorPageBase)this).CreateTagHelper<EnvironmentTagHelper>();
__tagHelperExecutionContext.Add((ITagHelper)(object)__Microsoft_AspNetCore_Mvc_TagHelpers_EnvironmentTagHelper);
__Microsoft_AspNetCore_Mvc_TagHelpers_EnvironmentTagHelper.set_Include((string)__tagHelperAttribute_1.get_Value());
__tagHelperExecutionContext.AddTagHelperAttribute(__tagHelperAttribute_1);
await __tagHelperRunner.RunAsync(__tagHelperExecutionContext);
if (!__tagHelperExecutionContext.get_Output().get_IsContentModified())
{
await __tagHelperExecutionContext.SetOutputContentAsync();
}
((RazorPageBase)this).Write((object)__tagHelperExecutionContext.get_Output());
__tagHelperExecutionContext = __tagHelperScopeManager.End();
((RazorPageBase)this).WriteLiteral("\r\n ");
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("environment", (TagMode)0, "c4eef6e21b532050217466a050cf103768022b7b10361", (Func<Task>)async delegate
{
((RazorPageBase)this).WriteLiteral("\r\n An unhandled exception has occurred. See browser dev tools for details.\r\n ");
});
__Microsoft_AspNetCore_Mvc_TagHelpers_EnvironmentTagHelper = ((RazorPageBase)this).CreateTagHelper<EnvironmentTagHelper>();
__tagHelperExecutionContext.Add((ITagHelper)(object)__Microsoft_AspNetCore_Mvc_TagHelpers_EnvironmentTagHelper);
__Microsoft_AspNetCore_Mvc_TagHelpers_EnvironmentTagHelper.set_Include((string)__tagHelperAttribute_2.get_Value());
__tagHelperExecutionContext.AddTagHelperAttribute(__tagHelperAttribute_2);
await __tagHelperRunner.RunAsync(__tagHelperExecutionContext);
if (!__tagHelperExecutionContext.get_Output().get_IsContentModified())
{
await __tagHelperExecutionContext.SetOutputContentAsync();
}
((RazorPageBase)this).Write((object)__tagHelperExecutionContext.get_Output());
__tagHelperExecutionContext = __tagHelperScopeManager.End();
((RazorPageBase)this).WriteLiteral("\r\n <a");
((RazorPageBase)this).BeginWriteAttribute("href", " href=\"", 931, "\"", 938, 0);
((RazorPageBase)this).EndWriteAttribute();
((RazorPageBase)this).WriteLiteral(" class=\"reload\">Reload</a>\r\n <a class=\"dismiss\">\ud83d\uddd9</a>\r\n </div>\r\n\r\n <script src=\"_framework/blazor.server.js\"></script>\r\n");
});
__Microsoft_AspNetCore_Mvc_Razor_TagHelpers_BodyTagHelper = ((RazorPageBase)this).CreateTagHelper<BodyTagHelper>();
__tagHelperExecutionContext.Add((ITagHelper)(object)__Microsoft_AspNetCore_Mvc_Razor_TagHelpers_BodyTagHelper);
await __tagHelperRunner.RunAsync(__tagHelperExecutionContext);
if (!__tagHelperExecutionContext.get_Output().get_IsContentModified())
{
await __tagHelperExecutionContext.SetOutputContentAsync();
}
((RazorPageBase)this).Write((object)__tagHelperExecutionContext.get_Output());
__tagHelperExecutionContext = __tagHelperScopeManager.End();
((RazorPageBase)this).WriteLiteral("\r\n</html>\r\n");
}
}
更多關於 Blazor 教學影片,可以參考 Blazor 教學影片播放清單 或者 Blazor 快速體驗教學影片撥放清單。也歡迎訂閱本 .NET / Blazor / Xamarin.Forms 影片頻道 。
在這篇文章中,將要來練習如何在 Blazor 專案內,顯示專案中的靜態圖片檔案;這些靜態的圖片檔案可以存在於該專案的網站根目錄下,也可以另外建立一個方案資料夾,宣告這個這料夾內可以儲存任何網站的靜態檔案,這也包括了圖片檔案。