Razor components vs Blazor - asp.net-core

I'm confused whats different between Razor components and Blazor and which is better and in the last release .NET Core 3.0 Preview 3 the add those to Razor Components
Razor Components improvements:
Single project template
New .razor extension
Endpoint routing integration
Prerendering
Razor Components in Razor Class Libraries
Improved event handling
Forms & validation

Essentially there are 3 parts to understand.
Razor Components
This is the name for the core, out of process, component model which was created back in July 2018, for the first release of Server-side Blazor.
Razor Components is the core of the framework and contains all the following things.
Parameters
Event handling
Data binding
Routing
Dependency injection
Layouts
Templating
Cascading values
Server-side Blazor
This is the server-side hosting model, running on ASP.NET Core, for Razor Components. This version hosts the Razor Components model on the server. It uses a small runtime to send UI events from the browser to the server. Once processed by Razor Components, any UI updates get sent back from the server to the browser and the runtime handles updating the DOM. All this communication is handled via a SignalR connection. Even JS interop calls are handled this way.
Client-side Blazor
This is the client-side hosting model for Razor Components.
In this model, everything is hosted in the browser. Mono, compiled to WebAssembly, is the .NET runtime. On top of this sits Razor Components and then finally the application.
The great thing about this architecture is that any feature added to Razor Components should, in theory, be available to both hosting models. Although in reality, this is not always the case.
What's better?
That very much depends on what you want to do.
Client-side Blazors biggest drawback is its download size. This alone could rule it out for many developers. Downloads are easily into multiple MBs which if someone is trying to view your app on a mobile with a slow connection, they are not going to have a great experience. However, it's worth noting that after the first download a lot of the content is cached so subsequent loads can be a few 100kb.
Client-side Blazors debugging experience is very primitive right now as well. Which means working on it as a developer can be challenging at times.
Server-side Blazor has a much nicer developer experience in terms of debugging. The app is much faster to download and only has a size of a few 100kb before any caching takes place.
The downside is potentially scalability. But this will very much depend on the number of concurrent users you are expecting. Because this model uses SignalR your app will have a top limit on concurrent connections. But, you can manage this by plugging into Azure SignalR to allow a far greater number of connections to your app.
Ultimately, both hosting models of Razor Components have a long way to go. The authentication stories for both are in very early days, although client-side Blazor is arguably in a better place. The routing engine is still limited, forms and validation have only just had it's first release and there is still work to do.
Another thing to keep in mind is that it is possible to swap between the models pretty easily. So whatever decision you make you're not tied into it. There will even be a way of doing this built into the framework at some point so nothing you do now will be wasted.
Any questions, please ask. But I hope this helps.

Razor Components is a framework with which you can create SPA web applications. It is divided into two mode of execution. When the web app is hosted and executed on the browser, it is called Blazor. Blazor applications are written in C# and compiled into .NET assemblies. They are executed and run on the browser as .NET assemblies by the Mono run time, which itself is compiled to Web Assembly.
The second mode of execution is server-side. That is, your web app is executed on the server, not on the browser. Note that here the runtime environment is not Mono Web Assembly but the Asp.net Core run time. This is called server-side Blazor, but the term Razor Components is used as well, so that the confused get perplexed. The reason for this is historic: At the beginning, there was only Blazor running on the browser. But then came the idea that a web app can run on the server, and only the diffs can be send to the browser by means of SignalR. Running the web app on the server is much easier than running on the browser, and the developer can use many elements he can't use on the browser, such as debugging, etc. As a result of this possibility, the Asp.Net renamed the Blazor framework as Razor Components, which you can treat as a super structure on which Blazor is built. This is why the confusion. Let's emphasize this division as follows:
Razor Components --> Blazor (front end; browser)
Razor Components --> Razor Components (server-side Blazor )
I know this is a source of confusion, but this is it...
As to the question which is better, I may only say that it depends exclusively on your requirements. Each of these mode of executions has its benefits and drawbacks. Blazor applications are more suitable for running on the internet as public web sites, whereas server-side Blazor application are best used on the Intranet as enterprise web sites.
The list you displayed is related to Razor Components framework. Some improvements, for the time being, may only be relevant to Blazor, others to server-side Blazor. There is only one way to know which is which: To learn Razor Components. It takes time to learn it, less than Angular, especially if you are a .Net developer, but it is still something which requires some investment.
Hope this helps...
I'll improve on it later on, but if you have specific question don't hesitate to ask...

You have every right to be confused, the naming has changed a lot and when you wrote the original question the Blazor team had recently renamed 'Server Side Blazor' as 'Razor Components'. That has thankfully since been abandoned, see the timeline below for more on that.
To anyone finding that the naming conventions in the answers here don't seem to marry up with what they are reading in older blog posts it's worth knowing that the meaning of 'Razor Components' has changed repeatedly over time.
This may also help anyone like me who has been using Blazor since near the beginning and feels sure that the names have changed!
TL; DR
The naming has evolved a lot during the pre-release period. Kudos to the Microsoft and the Blazor team for trying to come up with clear names and being willing to change back when needed. However, this has left a legacy of mixed naming conventions in older articles and some Blazor veterans sometimes use older naming conventions.
At the time of writing in September 2020, with Blazor at version 3.2, the official naming convention is:
Blazor Components (formally called 'Razor Components') can be used in...
Blazor Server apps, or ..
Blazor WebAssembly apps
The exciting history of Blazor naming
October 2018: 'Razor Components' becomes the new name for 'Blazor Server Side'
When Blazor 0.6.0 was released it was decided to officially name server side Blazor as 'Razor Components'.
Dan Roth discusses that in his Blazor 0.6.0 experimental release now available blog post in October 2018:
We announced last month at .NET Conf that we've decided to move
forward with shipping the Blazor server-side model as part of ASP.NET
Core in .NET Core 3.0. About half of Blazor users have indicated they
would use the Blazor server-side model, and shipping it in .NET Core
3.0 will make it available for production use. As part of integrating the Blazor component model into the ASP.NET Core we've decided to give
it a new name to differentiate it from the ability to run .NET in the
browser: Razor Components.
It's also discussed more in the ASP.NET Core updates in .NET Core 3.0 Preview 2 blog post.
Feb 19: Naming is hard...
Probably due to confusion that was arising, the Razor Components name for Server Side Blazor was expanded to 'ASP.NET Core Razor Components'. This is mentioned in the Blazor 0.8.0 release notes:
Server-side Blazor is now ASP.NET Core Razor Components in .NET Core
3.0 As was recently announced, server-side Blazor is now shipping as ASP.NET Core Razor Components in .NET Core 3.0. We've integrated the
Blazor component model into ASP.NET Core 3.0 and renamed it to Razor
Components. Blazor 0.8.0 is now built on Razor Components and enables
you to host Razor Components in the browser on WebAssembly.
April 2019: Switching back to Server-side Blazor
In April 2019 Blazor server side went into official preview and as part of that the naming for server side Blazor was switched back:
Simplifying the naming and versioning
For a while, we’ve used the terminology Razor Components in some cases, and Blazor in other
cases. This has proven to be confusing, so following a lot of
community feedback, we’ve decided to drop the name ASP.NET Core Razor
Components, and return to the name Server-side Blazor instead.
This emphasizes that Blazor is a single client app model with multiple
hosting models:
Server-side Blazor runs on the server via SignalR
Client-side Blazor runs client-side on WebAssembly
… but either way, it’s the same programming model. The same Blazor
components can be hosted in both environments.
Note that in the above description there is no mention of Razor Components at all, now we have two different Blazor hosting models (Client-side and server-side) as ways of delivering Blazor Components to the browser.
Sept 2019 The return of 'Razor Components'
Dan Roth's Blazor and .NET Core release notes for the next few versions no longer mention the term 'Razor Components' at all until .NET Core 3.0 Preview 9 when the term emerges again in the name of the 'Razor component unit test framework prototype'.
May 2020 'Razor Components' = 'Blazor Components'. Introducing 'Blazor Server' and 'Blazor WebAssembly'
By the time we get to May 2020 Razor Components and Blazor Components are now being used as synonyms for each other and the naming for the two hosting models has evolved.
The Blazor WebAssembly 3.2.0 now available blog describes that as follows (my emphasis):
Blazor components can then be hosted in different ways to create your web app. The first supported way is called Blazor Server. In
a Blazor Server app, the components run on the server using .NET Core.
And...
Blazor WebAssembly is now the second supported way to host your Blazor components: client-side in the browser using a WebAssembly-based .NET
runtime.
So.. the term 'Razor Components' is officially dead, right?
It would be so much easier if it was.. it does seem like 'Blazor Component' would be a more natural fit. But no, from the Components section of the official documentation:
Components in Blazor are formally referred to as Razor components.

Related

Confused about Blazor server default project

I'm confused about Blazor server default project.
About two years ago, I tried Blazor server 5.0 template created by Microsoft in Visual Studio 2019.
I see project layout has a 3 subprojects:
Blazor.Client contains the client side code
Blazor.Server contains ASP.NET Core Web API
Blazor.Share contains all shared data in 2 project above.
Now I need to create Blazor server project (version 7.0). When I created a project, there was only one project left.
And now, if we want to write something, we can write it in the initializeAsync method (database operation, file access etc.)
I want to know if this way is correct or not?
I don't need to write logic in Web API and use httpClient to request data, right?
Thanks for your answer, and sorry for my English.
About two years ago, I tried Blazor server 5.0 template created by
Microsoft in Visual Studio 2019. I see project layout has a 3
subprojects:Blazor.Client, Blazor.Server, Blazor.Share
You might misunderstand, The Blazor Server App template will build only one Blazor project. But the Blazor WebAssembly App template with selected the ASP.NET Core Hosted checkbox will generate a solution includes the following ASP.NET Core projects:
"Client": The Blazor WebAssembly app.
"Server": An app that serves the Blazor WebAssembly app and weather data to clients.
"Shared": A project that maintains common classes, methods, and resources.
More detail information, see ASP.NET Core Blazor project structure.
if we want to write something, we can write it in the initializeAsync
method (database operation, file access etc.) I want to know if this
way is correct or not?
Yes, you can access the database or file in the OnInitializedAsync() method.
I don't need to write logic in Web API and use httpClient to request
data, right?
It depends on your choice, by using Web API, you can directly access the data from the API, instead of from the components/web page, if you don't want to provide this API, you can create service layer or directly access the database or file in the OnInitializedAsync() method. Refer to this article: Blazor Server App CRUD With Entity Framework Core In .Net 5.

How would I update my project to use the new backend which is ASP.Net Core?

My current project uses the Ext JS framework and I am not familiar with how to update it to use the new backend which is ASP.Net Core. Where do I start from here?
I believe Ext.NET (https://ext.net/) would help you out.
Combining the leading client and server-side web technologies, including Ext JS and ASP.NET, Ext.NET brings a rock-solid component framework for web application development. Focus on creating your critical business logic while Ext.NET handles the web components for your applications. Its unparalleled cross-browser compatibility and advanced MVC architecture empower devs to create structured and scalable enterprise-grade applications.

Integrate ASP.NET Core Identity With Microsoft.Identity.Web in Blazor for Authentication

I have been working on authentication methods for my blazor app for some time now. I am currently developing as a standalone protected WebApi + Blazor Server, but will ultimately migrate to standalone protected WebApi + Hosted Blazor WASM so I need to be mindful of both server and client side authentication. The WebAPI will also serve an external OData feed and API for end users that also needs to be protected using the central authentication mechanism.
I would like to be able to sign on with Microsoft (ie. Microsoft.Identity.Web / MSAL), but want to configure some fairly complex roles and behaviours at the database level
(ie. ASP.NET Core Identity). To hopefully help someone else understand the different documentation sets, following are links to MS docs for the 2 options.
Introduction to Identity on ASP.NET Core
Microsoft Identity Platform Documentation
Don’t know about anyone else, but I have found it very difficult to navigate through the different documentation sets and tutorials to firstly understand how they work and secondly determine if it is best for me.
My question is, does anyone have any documentation on how they have integrated Microsoft.Identity.Web with the individual user accounts available in ASP.NET Core Identity for Blazor Server and/or WASM apps?, .
The following link shows how to do it all within ASP.NET Core Identity.
Integrate ASP.NET Core Identity with Microsoft account | BinaryIntellect Knowledge Base
If I was building an MVC web app, that’s what I would do. However, I really like the token handling capabilities of Microsoft.Identity.Web / MSAL (ie. ITokenAquisition etc.) for Blazor. It seems to be a real kludge to have to use the Razor pages for ASP.NET Core Identity and handling tokens securely becomes an issue – especially for Blazor WASM.
Chris Sainty has done some good work in porting some of the ASP.Net Razor pages to Blazor Components in the following link. However he does a lot of (very clever) manual processing of the token and I’m not sure I like the idea of storing the token in unprotected Local Storage of the blazor app. I haven’t gone into it in full detail yet, but I don’t think this method will be directly transferrable to Blazor WASM.
Authentication with client-side Blazor using WebAPI and ASP.NET Core Identity (chrissainty.com)
This SO post indicates that it is not possible to integrate Individual user accounts with Microsoft.Identity.Web.
c# - Microsoft Identity Plataform with asp.net Core Identity - Stack Overflow
I got a working solution going where I had both ASP.NET Core Identity and Microsoft.Identity.Web working side by side. However, I found this to be very difficult to implement and debug. Once you start mixing the various builder.Services.AddAuthetication(
) options (eg. .AddMicrosoftIdentityWebApp, . AddMicrosoftIdentityWebApi, .AddIdentityCore, .AddIdentity, .AddDefaultIdentity, .AddJWTBearer etc. etc.) I have found that you enter a world of pain and unpredictable behaviour. I basically had to go back to the source for each of them to work out what they were actually doing under the covers and work out how to blend them. I ended up going back to the raw OAuth / OpenId specifications and implementing everything manually – which is very unsatisfactory and I was unhappy with the risk I was taking in potentially introducing a security flaw – even though I got it to “work”.
EDIT: This SO post is similar to what I implemented. Microsoft Identity Local User Accounts and MSAL
I can’t believe how hard it has been to just get to this level of understanding, and still not have a solid working concept that does what I want it to do that is supported by documented acceptable techniques and not just my kludge at implementing everything manually.
Right now it appears to me that if I want to use as much out of the box / documented functionality as possible, I suspect that I should use ASP.NET Core Identity and work out how to integrate the ASP.net razor pages into my Web Api, Blazor Server and and Blazor WASM apps. However, this appears to be a backward step since Microsoft.Identity.Web / MSAL seems to be so much better suited to Blazor and seems to be the direction that MS is going.
If anyone can point me to some current examples of how this can be done, I would be very thankful.
I think I have found at least a partial answer to my question. The key problem I faced was how to capture the callback event from Microsoft.Identity.Web so that I can persist / retrieve info to/from the database during the authentication event. I was hung up on the ASP.NET Core Identity method of doing that.
I found the following SO post that provides information on how to respond to the OnTokenValidated event using Microsoft.Identity.Web. Microsoft.Identity.Web: OnTokenValidated event not triggered
Having access to this event means that I will be able to implement what I need to do at the database level and move on.

Why two projects in a solution with server-side Blazor

When one creates a new server-side Blazor project, you actually get two projects within a solution. Why is this? I suppose it has something to do with the architecture as shown in this diagram from the docs? So in other words, a .NET core process must be used to host the Blazor app. Is that the basic idea? In other words, IIS would serve up the .NET core app, which in turn "serves" up the Blazor app, or something to that effect?
According to "ASP.NET Core updates in .NET Core 3.0 Preview 2" blog article...
https://blogs.msdn.microsoft.com/webdev/2019/01/29/aspnet-core-3-preview-2/
Why two projects? In part it's to separate the UI logic from the rest of the application. There is also a technical limitation in this preview that we are using the same Razor file extension (.cshtml) for Razor Components that we also use for Razor Pages and Views, but they have different compilation models, so they need to kept separate. In a future preview we plan to introduce a new file extension for Razor Components (.razor) so that you can easily host your components, pages, and views all in the same project.

What is the equivalent of WebForms in ASP.NET Core?

I am doing my final year project in Computer Science and the project is a system for managing a college, like openSIS, but using ASP.NET Core to build it.
I need help on how to Add, Update, and Remove data using regular HTML forms, such as via <form/> and <input type="submit"/>. I knew how to do this in WebForms, but not in ASP.NET Core.
What you're asking to do is possible "out of the box" with ASP.NET Core and does not require a third-party solution.
Solution #1: ASP.NET Core Razor Pages
Microsoft - Introduction to Razor Pages
This is probably the most direct approach with the least to learn, and it is the modern equivalent to "Classic ASP.NET Web Forms." You are probably asking about this:
The basic idea is you create an "ASP.NET Core Web Application" project in Visual Studio and select the "Web Application" project template (confirming that ".NET Core" is selected in the template selection dialog before you click OK.)
You can then add "Razor Pages" containing HTML, CSS, and JavaScript, and use a simple post-back method via a regular HTML Form with a Submit Button. The link above has a topic on doing just that.
Solution #2: ASP.NET Core MVC
Microsoft - Overview of ASP.NET Core MVC
This is the second-most direct approach but does require you to grasp MVC concepts: Models, Views and Controllers. Implementing Razor Pages would only require you to grasp HTML and Razor Page code-behind, however, MVC as a pattern will provide your resulting project with a more maintainable and testable approach to the project.
The basic idea is to scaffold an "ASP.NET Core Web Application" project in Visual Studio, then select the "Web Application (Model-View-Controller)" project template.
Instead of adding Pages you add Views, to do anything useful these Views will have corresponding Controllers. There will also be Models defined, as you would see in a Razor Pages solution. In the case of MVC the Models are posted to your Controllers via Actions.
The link above covers the subject in detail, and there are numerous Stack Overflow posts on general MVC concepts.
Solution #3: RESTful Web APIs and Client-Side JavaScript
Microsoft - Building Web APIs
This is probably the least direct and most-technical approach. You will want to be familiar with "JavaScript" and "XHRs", "JSON" (or XML, but JSON has become de facto), and you will want to be versed in what it means to be a "RESTful Web Service" ie. that HTTP supports basic verbs to POST, GET, and DELETE a resource.
The basic idea is you create an "ASP.NET Core Web Application" project in Visual Studio using the MVC scaffold. You then implement Controllers to behave as RESTful web services. You can add Razor Pages and/or MVC Views to deliver the HTML and JavaScript.
With this approach you can create "Single-Page Applications" that never need to reload, where the HTML and JavaScript for the entire application can be delivered up-front and then RESTful web services used to modify the UI at the client. This can get advanced, and most developers will want to look into using a client-side templating engine. The leanest and simplest to use is probably going to be KnockoutJS, but others are more popular (and more complex.) Knockout has the value of solving a very specific problem: data-binding an HTML UI to JavaScript objects.
Regardless of the approach you take, start here: Microsoft - Get Started with ASP.NET Core
If you like the Web Forms way of development, or if you have a large ASP.NET Web Forms application and you need to implement new pages in it, you can try DotVVM.
It is not "Web Forms on .NET Core", but:
many concepts in DotVVM are similar to ASP.NET Web Forms (postbacks, server controls, master pages, even the names of the controls and page lifecycle events)
it is easy to learn for ASP.NET Web Forms developers
no cryptic viewstate hidden field
the controls don't produce ugly HTML
the MVVM pattern is used
no need to know or write JavaScript - C#, HTML and CSS is enough to start coding
DotVVM supports both .NET Core and full .NET Framework
can be added to existing ASP.NET Web Forms or MVC applications on .NET Framework
DotVVM is open source
Visual Studio extension with IntelliSense and project templates
Disclaimer: I am one of the authors of DotVVM. This post is not meant to advertise the project, I just believe it responds to the question because the main motivation to build DotVVM was the fact that Web Forms are not ported to ASP.NET Core, and many people is looking for the similar way of building web applications. DotVVM is not a port of Web Forms to .NET Core. Our intent is to build a framework which is conceptually similar, but which avoids the things Web Forms were criticized for (viewstate, testability and ugly HTML output).
DotVVM GitHub
Documentation
Gitter Chat
If you are referring to WebForms from ASP.NET (before MVC webstack came), there is no WebForms for ASP.NET Core and unlikely to ever be ported.
There is however a similar project (usually referred as Razor Pages or View Pages, see RazorPages GitHub repository) which allows to create Razor views which are not backed by a controller.
But as far as I know it's not release ready (there is no nuget package for it on nuget.org) and you'd have to use some previous from nightly builds or stable myget repositories.