HJ1990
HJ1990

Reputation: 415

Asp.net core - razor pages (.cshtml) vs angular 2+

I’ve been learning both asp.net core and angular, however I am a bit confused on what asp.net core razor pages are. I know that angular is a front-end framework to build dynamic applications and I know asp.net core is for the server side. However asp.net core also includes razor pages (.cShtml) that lets you write html code and also insert c# into it. Razor pages can also include partial views and so on but can you build a full website without using anything like react or angular and still have the same dynamic pages ? If I do use angular for the front end, are you able to still have the validation that comes from c# for forms and so on ?

Upvotes: 2

Views: 2242

Answers (3)

Niksr
Niksr

Reputation: 559

Dynamic pages that Razor allows to create are "dynamic" in terms of their content that can vary depends on app logic. But it's static as soon as page was rendered (appears on the screen). You need to refresh the whole page if you want to change just one single digit on this page.

Imagine you want to have a list of names on your page. You also have a form to add persons to that list on the same page. Naturally, you want to show a new name in the list immediately upon user presses a "submit" button. In Razor, you need to redraw the whole page. This is visible to the user because browser flickers and that is something that people neither want nor expect nowadays.

To address the issue of dynamic changes without refreshing a page you need to add JavaScript scripts to your Razor page. JS is something that able to talk directly with a browser. Angular/React/Vue/Blazor are essentially like JavaScript but offer more structured and simplified ways to achieve that goal.

Upvotes: 1

cl0ud
cl0ud

Reputation: 1614

Simply put - Razor is the view engine you would use in a classic MVC application which allows you to write server side code (C#) in the html (chtml) whereas angular is a Single Page Application framework on typescript.

You will find yourself still including certain javascript libraries in Razor or NPM packages using angular, it really broils down to the same thing.

The decision to use one or the other come down to a few factors such as but not limited to

  • Performance
  • Skill and Experience with the framework
  • Hosting

Generally you can use both to achieve what you are asking (building a site) but it would be ill advised to use both at the same time.

It seems the current trend is to use Angular as the clientapp and have .net core serve as the backend for business logic CRUD etc.

Upvotes: 1

Thomas Clague
Thomas Clague

Reputation: 537

Yes, you can build a dynamic site using Razor pages, You can use do like so

@foreach (var item in itemList)
{
   <p>@item</p>
}

If you decide to use angular, I would assume you will have a steeper learning curve and it will be harder to learn short term however to answer your second question, yes you can use the built in authentication by storing a JWT token and passing it in the headers for each request you make to the server

Edit: Angular 2+ is much better in my opinion as you have more flexibility in what you do but it has a lot more overhead work needed whereas razor will be quicker in the short term but will struggle with scale-ability

Upvotes: 1

Related Questions