[Ebooks PDF] download (Ebook) Building Single Page Applications in .NET Core 3: Jumpstart Coding Using Blazor and C# by Michele Aponte ISBN 9781484257463, 1484257464 full chapters
[Ebooks PDF] download (Ebook) Building Single Page Applications in .NET Core 3: Jumpstart Coding Using Blazor and C# by Michele Aponte ISBN 9781484257463, 1484257464 full chapters
com
https://ebooknice.com/product/building-single-page-
applications-in-net-core-3-jumpstart-coding-using-blazor-
and-c-11115594
DOWLOAD EBOOK
ebooknice.com
ebooknice.com
ebooknice.com
(Ebook) Learning Blazor: Build Single-Page Apps with
WebAssembly and C# by David Pine ISBN 9781492098416,
9781098113216, 1492098418, 1098113217
https://ebooknice.com/product/learning-blazor-build-single-page-apps-
with-webassembly-and-c-48681674
ebooknice.com
ebooknice.com
ebooknice.com
Michele Aponte
The publisher, the authors and the editors are safe to assume that the
advice and information in this book are believed to be true and accurate
at the date of publication. Neither the publisher nor the authors or the
editors give a warranty, expressed or implied, with respect to the
material contained herein or for any errors or omissions that may have
been made. The publisher remains neutral with regard to jurisdictional
claims in published maps and institutional affiliations.
During the Web 2.0 revolution, we had our first opportunity to port
desktop applications to the Web. Thanks to the Ajax technology, which
allowed us to do asynchronous calls to the server for the first time, we
no longer had to suffer through a page reload every time the user
updated the interface. We could finally get to the core of and resolve the
main problems of desktop application development.
With a complete server-side application, we no longer need to
install anything because we use the application through a browser,
simplifying the release of the updates and controlling the current
version used by our clients.
Unfortunately, all that glitters is not gold! If the user interface is
entirely built on the server side, moving the application to the server
has two distinct disadvantages. First, we must always be online to
contact the server, and second, all the computational effort for the
presentation layer passes from the customer’s computer to the server.
To solve these problems, we need to move the user interface
construction to the client. But if the application runs in a browser, we
need to write a substantial part of the code in JavaScript, and if you are
a .NET developer, this task has probably given you nightmares. If it has
not, in this chapter we’ll explore why it should with a simple example
that would frighten anyone coming from a strongly typed language.
Microsoft provided developers with a solution to these problems via
a front-end technology called Silverlight, together with a simplified
Windows Communication Foundation (WCF) back-end called Rich
Internet Application (RIA) services, that allowed us to use the .NET
Framework in the browser with the installation of a plug-in. Many
companies invested in this technology, but a few years later, Microsoft
decided to abandon the project, making those who today want to
approach Blazor somewhat gun-shy.
But Blazor is different. Blazor is based on standard technologies, not
Microsoft technologies. You don’t need to install anything on your
client, because the framework provides you with everything you need
to use .NET Core in the browser, taking advantage of what is already
there. If you are a Microsoft web developer and do not want to spend
your time learning JavaScript frameworks, Blazor is the solution for
you. I have helped many companies to adopt it successfully, and it has a
low learning curve and allows you to reuse your .NET Core knowledge.
In this chapter, we’ll work to overcome your fear of JavaScript and get
you on your way to creating your first Blazor application.
Code Nightmares
In all my JavaScript courses for .NET developers, I like to start the
lessons by creating an example.js file and writing the code shown in
Listing 1-1. This shows some JavaScript features that will surely
impress a C# or Visual Basic developer and immediately clarifies the
difficulties of a language so different from those .NET developers are
used to using.
function computes1() {
a = 10;
}
function computes2() {
a = 'hello'
}
computes1();
computes2();
console.log(a);
Listing 1-1 Some of the JavaScript Problems Summarized in a Single Script
Without executing the code, what is the result? Are you scared? If
you are not, you should be, because this code works, and the result is
hello. That means the variable a cannot be declared anywhere, its
scope is global, and its type can change without any problems from
number to string. In the computes2 function, I omitted the
semicolon because it is not required in JavaScript.
The language is case sensitive, so fullname and fullName are
different variables. If you cannot declare a variable and you fail to write
a variable name, the engine creates another global variable for you,
with an incredible loss of time in your debug sessions.
Note In JavaScript you can force the engine to check that variables
are declared with the "use strict"; directive (I see the smiles of
Visual Basic programmers), but it only comes in ECMAScript 5, so
some old browser will ignore it.
function computes() {
var a = 10;
if(a == '10') {
var b = 'ok';
}
console.log(b);
}
computes();
Listing 1-2 Some JavaScript Peculiarity for a .NET Developer
The execution result of the code is ok. Are you confused? The if
statement is true because in JavaScript the == operator executes the
type coercion between operands, converting the value of one operand
to the type of another. If you convert the value of the variable a from
the number 10 to the string ‘10’, the result of the condition is true. If
you do not want to allow this conversion, you can use the === operator.
The most interesting thing is that the b variable is declared in the
if block, so you could imagine that console.log(b) returns an
error both if condition is true and if it is false. Unfortunately, in
JavaScript, the scope of a declared variable is always at the function
level, not at the block level, so the b variable exists outside the if block.
Whether the if condition is false, which is the value of b? The
assignment of the ok string will be not executed, so its value will be
undefined. That is not null, but undefined, which is a possible
value of a JavaScript variable that represents the state of declared but
not initialized. I wish I could see your face right now!
Note In ECMAScript 6 you can use the keyword let instead of var
to declare a variable with block scope, but if your browser does not
support it, an error will be generated.
You Can Have Your Cake and Eat It Too with Blazor
Microsoft released the release-to-manufacturing (RTM) version of
Blazor with .NET Core 3, a new front-end framework that solves all the
problems previously mentioned. Thanks to it, you can use C# and the
.NET Core framework to write the front-end of your application, using
all the technologies you already know if you are a Microsoft web
developer.
You can use Razor, HTML, and C# to define the user interface and
use anything you want for the rest of the application. Blazor lets you
run the front-end directly in the browser, providing all the tools you
need to create a single-page application.
Blazor was created in 2017 as a personal project of Steve
Sanderson, who presented a preview of Blazor based on
DotNetAnywhere , a .NET Intermediate Language (IL) interpreter, at
NDC Oslo (https://www.youtube.com/watch?
v=MiLAE6HMr10&feature=youtu.be&t=31m45s). After this
presentation, Blazor was added to the ASP.NET GitHub repository as an
experimental project, but the enthusiasm of the community convinced
Microsoft to move the project to the ASP.NET team, replacing
DotNetAnywhere with Mono, which is the most famous open source
platform based on the .NET Framework (https://www.mono-
project.com/).
With the .NET Core 3 release, Blazor has become part of the
framework, with an ambitious roadmap. As you can see in Figure 1-1, at
the moment you can create the front-end of a web application with
Blazor, but the idea is to eventually be able to build desktop and mobile
applications with it, going through a progressive web app (PWA)
approach as an intermediate step.
Blazor Server is the version that ships with .NET Core 3, and it
allows you to prerender the HTML of your application, execute the C#
code on the server side, and push the user interface changes to the page
through SignalR. Blazor WebAssembly is available from May 2020, and
it executes the C# code directly in the browser. You can use Blazor
WebAssembly with .NET Core 3.1.300 or later.
Blazor Hybrid will be a native .NET renderer to Electron and
WebView, and it will be a native app that works online and offline.
Electron (electronjs.org) is a popular open source project to
create cross-platform desktop applications using web technologies. As
an example, Visual Studio Code is based on Electron. Blazor Native, on
the other hand, will have the same programming model but without
HTML rendering. In this book, we talk about Blazor Server and Blazor
WebAssembly because they are the only confirmed projects with
precise dates of release, but Microsoft has long-term plans for this
technology, so there’s no time like the present to learn it.
@page "/"
@namespace countdown.Pages
<!DOCTYPE html>
<html lang="en">
<head>
<title>Countdown App</title>
</head>
<body>
@(await Html.RenderComponentAsync<Countdown>(
RenderMode.ServerPrerendered))
<script src="_framework/blazor.server.js">
</script>
</body>
</html>
Listing 1-5 Blazor Server Host Page
Blazor uses the same component concept as all modern UI
frameworks, in which a set of pieces, called Blazor components,
composes the user interface like in a puzzle. A Blazor component is,
therefore, a reusable piece of your user interface that can contain both
HTML (with its C# code) and other Blazor components.
I will talk about components in forthcoming chapters; for now, think
of them as reusable pieces of your user interface. The
RenderComponentAsync() method renders the component
indicated in its generic parameter (Countdown in our case) with a
server prerendered modality. This method of rendering a component is
a peculiarity of Blazor Server and is not used, for example, in Blazor
WebAssembly; we will talk about the differences in depth in Chapter 2.
The script _framework/blazor.server.js loads the
JavaScript code of Blazor that permits the communication with the
server. Note that to permit the loading of the script, we need to invoke
the app.UseStaticFiles() method in the Startup class (see
Listing 1-4).
It’s time to create our first Blazor component! Let’s create a file
named Countdown.razor in the root folder. Our goal is to create a
component that implements a simple countdown from 10 to 0 when
the user clicks a Start button. Let’s start with an intermediate step in
which we define the user interface and initialize the countdown when
someone clicks the Start button. See Listing 1-6.
@using Microsoft.AspNetCore.Components.Web
<h1>Countdown</h1>
<p>@count</p>
<button @onclick="startCountdown">Start</button>
@code {
private int count = 0;
private void startCountdown()
{
count = 10;
}
}
Listing 1-6 Countdown Razor Component Start Code
@code {
private int count = 0;
Summary
In this first chapter, I talked about why Blazor is a viable solution for
.NET developers who need to create a modern web application with a
rich user interface without taking the time to learn the JavaScript
language and frameworks.
You also learned that the first version of Blazor was released with
.NET Core 3 and that a library ecosystem and complex use cases are not
yet available but are forthcoming. In addition, you learned that
Microsoft’s vision for this technology is long-term, and the company is
paying great attention to the use of web standards rather than
proprietary technologies.
In the next chapter, I will cover how Blazor works internally and the
main differences between Blazor Server and Blazor WebAssembly so
you know which to pick for your needs.
© Michele Aponte 2020
M. Aponte, Building Single Page Applications in .NET Core 3
https://doi.org/10.1007/978-1-4842-5747-0_2
As I always say, there is not one tool that does everything but instead different tools for
different requirements. A good programmer chooses his tools solely according to the
requirements. You have to remember that requirements can be functional and
nonfunctional, and often nonfunctional requirements are more important than
functional ones for the success of an application.
You might think that Microsoft released Blazor Server before Blazor WebAssembly
just because the latter was not ready yet; however, as you will see in this chapter, Blazor
Server and Blazor WebAssembly solve the same problem with different approaches. You
must choose which one will work best for you depending on your requirements.
THE END.
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebooknice.com