title
stringlengths
3
46
content
stringlengths
0
1.6k
19:620
</InputRadioGroup>
19:621
19:622
Finally, Blazor also offers an InputFile component together with all the tools for processing and uploading the file. We will not cover this here, but the Further reading section contains links to the official documentation.
19:623
The next section describes Blazor tools for modifying the host page <head> tag.
19:624
Modifying HTML <head> content from Blazor components
19:625
Since the whole component tree is placed inside the body of the index.html host page, the components markup has no direct access to the index.html host page <head> tag. Modifying the content of the <head> tag is necessary when the developer would like to adapt the title shown in the browser tab to the actual Blazor page that is being displayed. In fact, this title is contained within the <head> tag:
19:626
<head>
19:627
<title>This is the title shown in the browser tab</title>
19:628
...
19:629
</head>
19:630
19:631
For this reason, the .NET 6 version of Blazor introduced specific constructs for modifying the host page <head> tag from inside Blazor components.
19:632
First of all, we must inform the Blazor application about how to reach the <head> tag content. This is done in Program.cs with the same technique used to specify the Blazor application root:
19:633
var builder = WebAssemblyHostBuilder.CreateDefault(args);
19:634
builder.RootComponents.Add<App>(`#app`);
19:635
// The line below adds support for modifying
19:636
// the <head> tag content
19:637
builder.RootComponents.Add<HeadOutlet>(`head::after`);
19:638
...
19:639
19:640
After that, each component can replace the HTML title by specifying the new string inside a PageTitle component instance:
19:641
<PageTitle>This string replaces the page title</PageTitle>
19:642
19:643
Moreover, each component can append other HTML content to the <head> tag by placing it inside a HeadContent instance:
19:644
<HeadContent>
19:645
<meta name=`description` content=`This is a page description`>
19:646
</HeadContent>
19:647
19:648
In this section, you learned all that is needed to write a simple Blazor application that doesn’t exchange data with a server. The next section analyzes some advanced features and will enable you to interact with a server to handle authentication, authorization, and more.
19:649
Blazor advanced features
19:650
This section provides short descriptions of various Blazor advanced features organized into subsections:
19:651
19:652
References to components and HTML elements
19:653
JavaScript interoperability
19:654
Globalization and localization
19:655
Authentication and authorization
19:656
Communication with the server
19:657
AOT compilation
19:658
19:659
Because of a lack of space, we can’t give all the details of each feature, but the details are covered by links in the Further reading section. We start with how to reference components and HTML elements defined in Razor markup.
19:660
References to components and HTML elements
19:661
Sometimes, we might need a reference to a component in order to call some of its methods. This is the case, for instance, for a component that implements a modal window:
19:662
<Modal @ref=`myModal`>
19:663
...
19:664
</Modal>
19:665
...
19:666
<button type=`button` class=`btn btn-primary`
19:667
@onclick=`() => myModal.Show()`>
19:668
Open modal
19:669
</button>
19:670
...
19:671
@code{
19:672
private Modal myModal {get; set;}
19:673
...
19:674
}
19:675
19:676
As the preceding example shows, references are captured with the @ref directive. The same @ref directive can also be used to capture references to HTML elements. HTML references have an ElementReference type and are typically used to call JavaScript functions on HTML elements, as explained in the next subsection.
19:677
JavaScript interoperability
19:678
Since Blazor doesn’t expose all JavaScript features to C# code, and since it is convenient to take advantage of the huge JavaScript code base, sometimes it is necessary to invoke JavaScript functions. Blazor allows this through the IJSRuntime interface that can be injected into a component via dependency injection.
19:679
Once we have an IJSRuntime instance, a JavaScript function that returns a value can be called as shown here:
19:680
T result = await jsRuntime.InvokeAsync<T>(
19:681
`<name of JavaScript function or method>`, arg1, arg2....);
19:682
19:683
Functions that do not return any argument can be invoked as shown here:
19:684
await jsRuntime.InvokeAsync(
19:685
`<name of JavaScript function or method>`, arg1, arg2....);
19:686
19:687
Arguments can be either basic types or objects that can be serialized in JSON, while the name of the JavaScript function is a string that can contain dots that represent access to properties, sub-properties, and method names, such as the `myJavaScriptObject.myProperty.myMethod` string.
19:688
Thus, for instance, we can save a string in the browser’s local storage with the following code:
19:689
await jsRuntime
19:690
.InvokeVoidAsync(`window.localStorage.setItem`,
19:691
myLocalStorageKey, myStringToSave);
19:692
19:693
Arguments can also be ElementReference instances captured with the @ref directive, in which case they are received as HTML elements on the JavaScript side.
19:694
The JavaScript functions invoked must be defined either in the Index.html file or in JavaScript files referenced in Index.html.
19:695
If you are writing a component library with a Razor library project, JavaScript files can be embedded together with CSS files as resources in the DLL library. You just need to add a wwwroot folder in the project root and place the needed CSS and JavaScript files in that folder or some subfolder of it. After that, these files can be referenced as follows:
19:696
_content/<dll name>/<file path relative to wwwroot>
19:697
19:698
Accordingly, if the filename is myJsFile.js, the DLL name is MyCompany.MyLibrary, and the file is placed in the js folder inside wwwroot, then its reference will be:
19:699
_content/MyCompany.MyLibrary/js/myJsFile.js
19:700
19:701
It is worth pointing out that all CSS files added to components (CSS isolation) we mentioned earlier in this chapter are compiled into a unique CSS file that is added as a DLL resource. This file must be referenced in the index.html page as:
19:702
<assembly name>.Client.styles.css
19:703
19:704
If your JavaScript files are organized as ES6 modules, you can avoid referencing them in Index.html and can load the modules directly, as shown here:
19:705
// _content/MyCompany.MyLibrary/js/myJsFile.js JavaScript file
19:706
export function myFunction ()
19:707
{
19:708
...
19:709
}
19:710
...
19:711
//C# code
19:712
var module = await jsRuntime.InvokeAsync<JSObjectReference>(
19:713
`import`, `./_content/MyCompany.MyLibrary/js/myJsFile.js`);
19:714
...
19:715
T res= await module.InvokeAsync<T>(`myFunction`)
19:716
19:717
In addition, instance methods of C# objects can be called from JavaScript code, by taking the following steps:
19:718
19:719
Say the C# method is called MyMethod. Decorate the MyMethod method with the [JSInvokable] attribute.