Repository navigation
ASPNET Core 5 MVC Bootstrap Responsive Web Programming Lecture 1 First Web Application
Full tutorial link > https://www.youtube.com/watch?v=77iDUQd4x90
#SoftwareDevelopment #Software #Engineering #CSharp #Coding #Course
Asp.NET Core V5 - MVC Pattern - Bootstrap V5 - Responsive Web #Programming with C# Full Course Playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffsnAWtgk4ja3HN3xgMKF7BOE
GitHub repository of the course : https://github.com/FurkanGozukara/Responsive-Web-Design-With-ASP.NET-5-and-MVC-Pattern-and-BootStrap-5-2021
Discord channel link of the course : https://discord.gg/6Mrb8MwteQ
How to use Discord : https://youtu.be/AEwPtYiLvsQ
In Lecture 1:
-
What is MVC (model-view-controller) pattern
-
Whare The Model, The View and The Controller
-
What is ASP.NET MVC?
-
The ASP.NET MVC View Engine
-
What's the "Core" part?
-
ASP.NET MVC vs. Web Forms
-
Where Web Forms Failed
-
How to compose a ASP.NET Core v5 MVC project
-
How to compose a controller and edit it
-
How to compose a view (a Razor view) and edit it
-
How to compose a model and use it / access it inside the view
-
What is Razor view engine?
-
Why use Razor?
-
How input is encoded in Razor view
-
Explicit expressions in Razor view
-
Multi-statement Razor blocks
-
How to write comments in Razor syntax
Please subscribe and make comments.
This course requires fundamental knowledge about programming and C#. So please watch our below playlists related to C#.
Our courses playlists are like below:
[1] Introduction to Programming Full Course with C# playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffskoSXySh0MdiayPJsBZ7m2o
[2] Advanced #Programming with C# Full Course Playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffslHaBdS3RUW26RKzSjkl8m4
[3] Object Oriented Programming Full Course with C# playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffsnH3XJb66FDIHh1yHwWC26I
[4] Asp.NET Core V5 - MVC Pattern - Bootstrap V5 - Responsive Web #Programming with C# Full Course Playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffsnAWtgk4ja3HN3xgMKF7BOE
[5] Artificial Intelligence (AI) and Machine Learning (ML) Full Course with C# Examples playlist: https://www.youtube.com/playlist?list=PL_pbwdIyffskVschrADCL6KEnL_nqDtgD
[6] Software Engineering Full Course playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffslgxMVyXhnHiSn_EWTvx1G-
[7] Security of Information Systems Full Course playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffslM_o92NwkaUzD7C6Fekx26
[8] (Turkish) Bilgisayar Becerileri Tam Ders playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffsmyE2e909ea1MXLcMb8MenG
auto subtitle timing : NA
-
00:00:00 Hello dear students. Welcome to the Lecture 1 of Responsive Web Design with ASP.NET 5
-
00:00:06 and MVC Pattern and Bootstrap 5. So,
-
00:00:14 we will proceed this course with using C Sharp (C#) language, programming language,
-
00:00:23 and Microsoft SQL Server. So, what you need for this course is Visual Studio Community Edition
-
00:00:36 with installing C Sharp and
-
00:00:42 ASP.NET. You have to install them and also you need to install Microsoft SQL Developer Edition
-
00:00:57 and Microsoft SQL Management Studio. So, if you install these, you will be able to
-
00:01:08 follow the course as I progress. So, in this course, we will be using our GitHub repository
-
00:01:19 that I have composed for this year's course. Responsive Web Design with ASP.NET 5 and MVC
-
00:01:26 pattern. Here the URL, I will also put this URL into the description of video. Also, you can type
-
00:01:35 Furkan Gozukara for GitHub, and from repositories, you can find the repository from there as well.
-
00:01:46 So, here my Visual Studio. It is installed. Actually, we will be using ASP.NET Core.
-
00:01:53 However, it is named as ASP.NET after certain update. We will be using version 5.
-
00:02:05 OK, so what else to be said? So, let's start with initializing our repository.
-
00:02:16 OK, what is the folder name? OK, here you see it is empty right now. I will initialize it.
-
00:02:37 Git add remote origin, if I remember correctly.
-
00:02:46 Maybe like this. Yes, I have added our repository and
-
00:02:56 now I will synchronize my folder. OK, now we have synchronized folder for our
-
00:03:07 course material here. And also I will add gitignore
-
00:03:16 file as well and another folder that I will be using. OK, let's name it as. OK, let's start. So,
-
00:03:29 I will start with composing a new project. And let's search for our project template. OK.
-
00:03:40 You see there is ASP.NET Core web application and ASP.NET web application .NET Framework.
-
00:03:48 They have named the .NET Core as .NET in C-Sharp WPF application as you can see here.
-
00:03:56 This is .NET Core and this is the older .NET Framework. However, it appears that ASP.NET
-
00:04:02 Core is still named as .NET Core. Therefore, do not use ASP.NET web application. Use ASP.NET Core
-
00:04:11 web application and then we will select the MVC. You see here it is showing there. So,
-
00:04:20 this is the project template that we will be using for this course. OK, and let's pick our folder.
-
00:04:33 OK, here. Let's give the project name lecture 1.
-
00:04:41 OK, for this course I will be using the tutorials on the
-
00:04:51 internet because they are extremely useful. I think the good one was. Let me find the good
-
00:05:03 tutorial. We will check every one of them pretty much, but OK, this one was a pretty good one.
-
00:05:12 OK, so asp.mvc.tutorial.com. OK, you see now it is telling us to pick the version.
-
00:05:22 We will be using ASP.NET Core 5 latest version. And you see we can also pick the
-
00:05:29 framework type here .NET Core or .NET Framework. If you wonder the difference between .NET Core
-
00:05:35 and .NET Framework, .NET Core can work on other platforms as well such as Linux, macOS, and such.
-
00:05:46 So, there is Empty, API, Web Application, Web Application (Model-View-Controller),
-
00:05:51 and such. OK, let's continue with the tutorial. I think it was composing an empty one.
-
00:06:01 OK, let's continue. So, let's start with what is MVC. MVC, short for Model-view-controller,
-
00:06:13 is a software pattern originally developed in the 1970's when graphical user interfaces were in the
-
00:06:19 very early phases of development. MVC strives to separate an application into three areas:
-
00:06:26 OK, so MVC is Model View Controller and it is a design pattern that can be applied
-
00:06:33 to all object-oriented programming languages. So, there are the model. OK, so let's read the model.
-
00:06:44 This is where data and business logic is stored. In a typical web application, this part of the
-
00:06:50 application would represent e.g. a database table or any other object that your application should
-
00:06:55 deal with. And then there is view. The View is the actual visual representation of your Model.
-
00:07:04 In a typical web application, this would be the page that shows your Model to the user,
-
00:07:08 whether it be a form for inputting the data or an output of the data or a combination of both.
-
00:07:14 Obviously, a View doesn't have to show all parts of the Model, and a Model can have more
-
00:07:19 than one View. And then the controller. OK, let's read the controller as well.
-
00:07:25 The Controller should handle all communication between your Model and your View.
-
00:07:30 In a typical web application, the methods of the Controller are invoked by the user when a page is
-
00:07:35 loaded or a button is clicked. The Controller then updates the Model, if necessary, and then
-
00:07:41 returns a new View (or action, but more about that later), if necessary. So, we separate our
-
00:07:48 design into three layers. The model, where we handle the data, such as we contact the database
-
00:07:57 and modify, update, select, insert, delete the data. And then we have the view, where we show
-
00:08:07 the data to the user. It is about the graphical user interface. And then we have
-
00:08:14 the controller, where the communication between model and view is made. The user controls
-
00:08:23 our application. So this can be applied to any programming languages that supports it.
-
00:08:31 For example, if we give an example on Android mobile application development,
-
00:08:37 you can use the same model in an Android application and in a web application. OK,
-
00:08:46 because it is the background thing. What would change is the view and the controller would change
-
00:08:51 according to the platform you are developing. So this can make our job much, much easier if
-
00:08:58 you are developing multi-platform applications. Also, this is the future of the development.
-
00:09:08 Let's say philosophy, maybe we can call as that. So MVC is the model
-
00:09:19 and design approach that you should follow in your professional life. So the rise of the MVC
-
00:09:26 is. After being introduced in the 1970's, the MVC pattern didn't get much attention for the next 30
-
00:09:35 years. Early web languages, like Perl, PHP and ASP Classic didn't really focus on design patterns and
-
00:09:42 even with the release of ASP.NET, MVC was still not an integrated part of it. Sure, you could
-
00:09:49 apply MVC techniques to your projects, but none of the popular frameworks encouraged it in any way.
-
00:09:57 Ok then. Then, in 2004, the Ruby on Rails framework was released, which was a web
-
00:10:06 development framework based on the Ruby language with heavy inspiration from the MVC pattern.
-
00:10:12 A lot of people believe that this put the focus back on MVC and shortly thereafter,
-
00:10:17 many PHP frameworks, centered around the MVC pattern, was released. All of this made the
-
00:10:23 MVC pattern hugely popular, which is probably why Microsoft decided to do an implementation
-
00:10:29 of it for their .NET framework, including massive tool support in their Visual Studio IDE. ASP.NET
-
00:10:36 MVC was released in 2007, but the first final version didn't see the light of day before 2009.
-
00:10:45 In the next chapters, we will look deeper into what ASP.NET MVC is all about and how it compares
-
00:10:51 to the alternatives, mainly ASP.NET WebForms. To be frank, I have developed my applications so
-
00:11:01 far in ASP.NET web forms because I didn't have the enough technical knowledge and experience
-
00:11:07 when I first started my web-based applications, web-based games actually. However, I am so much
-
00:11:19 regretting that I didn't use the MVC pattern in my development. Because, if I had used it, I could
-
00:11:27 also make another platform application for my web-based games with very little amount of work.
-
00:11:38 So, what is ASP.NET MVC? We previously talked about MVC in general, but in this chapter,
-
00:11:46 we will be focusing on using the MVC pattern for building ASP.NET MVC web applications.
-
00:11:53 Microsoft introduced ASP.NET MVC in 2007, with the first stable release being available in 2009.
-
00:12:02 It's worth noting that the MVC implementation is actually open source - Microsoft has released
-
00:12:08 the entire MVC framework under the Apache License 2.0, which allows you to view and
-
00:12:13 modify the framework and even redistribute your changes. Okay, so you see it is open source and
-
00:12:20 open source means that it has a future. Why? Because everyone can contribute and
-
00:12:30 it will progress much faster than a closed source code application
-
00:12:35 and I think it is better it to be an open source. So, the ASP.NET MVC View engine.
-
00:12:48 ASP.NET MVC was originally constructed to use WebForms (the original ASP.NET technology)
-
00:12:54 as its views, but in later versions, it has been made possible to easily change the view engine
-
00:12:59 used by ASP.NET MVC to use custom-built engines and Microsoft even developed one,
-
00:13:05 called Razor, which was released together with ASP.NET MVC version 3 in 2011.
-
00:13:12 Today, Razor is the most commonly used view engine, but besides WebForms, several alternative
-
00:13:18 engines have been developed by the community, like Brail, NDjango, SharpTiles and many more.
-
00:13:24 In this tutorial, we will focus on the Razor view engine because it's really good and easy
-
00:13:28 to get started with! Yes, I would also say that the Razor engine is one of the best.
-
00:13:35 So, we will be using the Razor engine. It is a good one and let's continue. So,
-
00:13:41 what is the core part? You see, we could use ASP.NET framework or ASP.NET core. So,
-
00:13:48 let's read more information about core. You have probably already noticed that this tutorial
-
00:13:55 is called the "ASP.NET MVC Core Tutorial", but why Core? The original .NET framework,
-
00:14:02 along with ASP.NET components, was originally released as a closed-source framework in 2002.
-
00:14:08 Later on, Microsoft decided that they wanted to create an open-source .NET framework with
-
00:14:14 support for the major operating systems: Windows, OS X and Linux. They called this the ".NET Core
-
00:14:21 framework" and it was released in 2016, followed by many rapid releases with lots of improvements.
-
00:14:28 After the release of.NET core, the rise of C-sharp and .NET has dramatically increased. It has
-
00:14:40 caused and hype in the development community. It has definitely given a life to do .NET programming
-
00:14:50 languages. So, I think it is because of the correcting. Today, the .NET Core framework is
-
00:14:59 just as stable as the original .NET framework and besides compatibility with more operating systems
-
00:15:05 it also comes with much better performance and a faster release cycle, meaning that you
-
00:15:09 will get access to bug fixes and new features faster than with the original .NET framework.
-
00:15:15 So, unless you need legacy functionality found only in the original .NET framework,
-
00:15:20 you should always go with the .NET Core version! This is so true because basically there is
-
00:15:28 almost no differences between .NET framework programming and .NET core programming. Therefore,
-
00:15:34 why not use the latest technology? Why not use the technology of the future? OK, so.
-
00:15:43 Summary: Now that you know a bit more about MVC and the .NET flavor of it,
-
00:15:47 move on to the next article so we can discuss how ASP.NET MVC stack up to the original ASP.NET
-
00:15:54 view engine: ASP.NET WebForms. OK, let's continue. So, ASP.NET MVC versus Web Forms. OK.
-
00:16:04 As I said, I have a great knowledge and experience on web forms because I have developed my
-
00:16:14 license thesis with them and there are two games that I have been developing.
-
00:16:19 For example, the major one is Monster MMORPG. This is a web forms-based development, ASP.NET,
-
00:16:28 and I have written probably over 1 million lines of code so far in 11 years, actually
-
00:16:38 12 years now almost. I have been developing since 2009. Yes, you see correctly. So,
-
00:16:47 I have great experience and knowledge in web forms, but I can say that the .NET core MVC is
-
00:16:56 much better, believe me. So, you should always go with ASP.NET MVC web form, ASP.NET MVC,
-
00:17:05 and razor pages if you are going to develop a web application. I can say that
-
00:17:11 this is the best programming language for now, at the moment for developing web applications.
-
00:17:20 So, the game has so many features. If you log in, register, and log in,
-
00:17:25 you can check them out if you want. Anyway, let's continue to our subject. The first version of
-
00:17:35 ASP.NET was released in 2002, with the Web Forms view engine being the only choice available.
-
00:17:42 Later on, to support stuff like MVC, Microsoft has extended ASP.NET to support multiple view engines,
-
00:17:49 but for many years, if you were using ASP.NET you were also automatically using Web Forms.
-
00:17:58 Microsoft had a very commendable goal when they created Web Forms:
-
00:18:02 They wanted to abstract as much of the gritty details of the HTTP protocol and
-
00:18:07 the stateless nature of it away and make web development feel much more like developing
-
00:18:11 a Windows application, which was already a pretty pleasant experience at the time.
-
00:18:17 They did so by introducing the ViewState, which would make sure that the current state of any
-
00:18:21 form was preserved during post backs to the server and they did it with server controls,
-
00:18:26 which encapsulated the rendering of HTML and CSS into an arbitrary control which you could
-
00:18:31 customize using logical properties instead of being forced to mix HTML and CSS directly.
-
00:18:42 They also introduced the event driven model, already known to the Windows developers at
-
00:18:47 that time, to allow the developer to respond to actual events, like a button being clicked or a
-
00:18:52 checkbox being changed, instead of doing manual checks for this each time the page was loaded.
-
00:18:58 This also meant that markup and code was separated, which in theory is a great thing.
-
00:19:04 Okay, where web forms failed? Web Forms was a fresh breath of air for many developers,
-
00:19:12 and it likely also helped a lot of new developers, or developers only familiar
-
00:19:17 with Windows application development, to learn building applications for the web.
-
00:19:22 Unfortunately Microsoft didn't succeed in creating the perfect and flawless abstraction,
-
00:19:27 because a number of problems quickly emerged. Some of them were fixed in later releases,
-
00:19:32 while others were more fundamental to the way WebForms worked and therefore harder to change.
-
00:19:38 The Web Forms technology has been criticized mainly for the following things:
-
00:19:42 Okay, ViewState makes pages heavier. This is so true. I will show you what does this mean in my
-
00:19:50 web forms-based application. So, when we open the MonsterDex page, which has a lot of,
-
00:20:00 let's say, features, we can see the ViewState of the page. Okay, when I open the head,
-
00:20:16 I think it was somewhere here. One moment.
-
00:20:46 Not head but.
-
00:20:51 Okay, inside the form, okay here. You see there is div class ASP.NET hidden and inside here,
-
00:20:58 you will see the ViewState. You see how many ViewStates there are.
-
00:21:06 You see like there are 82 ViewStates and all of these text is sent to the server and get back
-
00:21:16 each time you make a request to the server. Can you imagine how much overload it is adding to the
-
00:21:25 application? From network view, let me show you. So, let's say I want to filter by a name and when
-
00:21:35 I click this, now we will see the post. Okay, here is the post and let's see what has been posted
-
00:21:44 and what we did get. So, the form data you see, we have posted all these ViewStates.
-
00:21:54 And here we have got all this as a response and inside the response,
-
00:22:02 I think we can see the ViewState somewhere.
-
00:22:11 Okay, here at the bottom, it is a very long ViewState you see. So, this is the maybe the worst
-
00:22:19 part of the ASP.NET web forms. Okay. It increases your overload a lot
-
00:22:31 and it slows down, of course, the application, and let's see the
-
00:22:39 post data size and the response data size. Okay, so the content length is 47 kilobytes. Okay,
-
00:22:50 just for doing an action, I am sending 47 kilobytes and maybe we can see better from here.
-
00:23:06 The post. Okay, so you see the transferred over network size is 80 kilobytes. It is huge
-
00:23:17 to just do an action and this is the worst part of web forms.
-
00:23:27 By keeping track of every server control on the page in a ViewState string, which
-
00:23:31 is sent back and forth between the server on each request, Web Forms pages got quite a bit heavier.
-
00:23:37 If you were building a medium complex page, the resulting ViewState string could lead to an
-
00:23:42 increase of several hundreds of kilobytes. This could lead to longer load times,
-
00:23:48 especially on a mobile connection and with the increase of traffic from smartphones
-
00:23:52 all over the world, this became a very real problem. Yes, this is a so true sentiment.
-
00:24:00 Therefore, we should avoid web forms if possible. Server controls limits your control over HTML
-
00:24:09 output. Server controls makes it easy for you to quickly create something useful, but you never get
-
00:24:16 full control of the HTML which it renders. This can become a problem when you need to
-
00:24:21 fine-tune your work as well as if you experience browser compatibility problems. Yes, this may be
-
00:24:27 a problem, but not that much. I think the worst problem is ViewState. Also, when you depend on the
-
00:24:37 ASP.NET web forms provided asynchronous events, you really increase the
-
00:24:45 back-and-forth send data. That is another worst part. It is not lightweight as you are using AJAX
-
00:24:55 with like JQuery or such or JavaScript itself. And let's continue.
-
00:25:02 Web Forms is bad for automated testing. The Web Forms model was introduced before automated/unit
-
00:25:10 testing became a big thing and when it did, it was easy to see that Web Forms was hard,
-
00:25:15 if not impossible, to effectively unit test. This is also true because
-
00:25:22 you don't separate layers and therefore it is harder to do automated testing as being layered.
-
00:25:32 In the modern programming, testing is alone a profession. Companies are hiring testers to test
-
00:25:41 their applications between updates or before release. And this is true. Let's continue.
-
00:25:49 Where ASP.NET MVC is an improvement over web forms. Okay, let's read the advantages
-
00:25:56 of ASP.NET MVC. ASP.NET MVC removes a lot of the abstractions implemented by Web Forms.
-
00:26:04 For instance, you usually generate all of the HTML yourself, instead of relying on server controls.
-
00:26:11 There is also no longer any ViewState maintained for you,
-
00:26:14 effectively eliminating that problem (but also rendering several of the server controls,
-
00:26:19 like the GridView and the Repeater, useless at the same time). The MVC model is perfect
-
00:26:26 for automated/unit testing, because of the loose coupling between the different layers.
-
00:26:31 Okay, so which technology should you choose? Of course, ASP.NET MVC Core. But let's read it as
-
00:26:40 well. It's important to state that while Web Forms may seem like an outdated technology when reading
-
00:26:47 the above, it's actually not at all - Web Forms is still being actively developed by Microsoft
-
00:26:52 and is still a possible choice when entering the world of web development with ASP.NET.
-
00:26:58 Web Forms is especially well suited for situations where you want something up and running in a
-
00:27:03 hurry - the big amount of advanced server controls makes it easy to accomplish something very useful
-
00:27:08 in a rush, at the price of the flexibility it gives you to write all the markup manually.
-
00:27:14 Yes, it is true. If you are in a such hurry, you can use it, but I strongly
-
00:27:22 say that don't use it. Because after a certain point, it is impossible to
-
00:27:28 change between applications. It's a true hard thing because let's say I decided to convert my
-
00:27:36 web forms application into ASP.NET MVC Core. Then I have to do maybe thousands of tests
-
00:27:47 total to make sure that application is working correctly. Because when doing a programming,
-
00:27:55 you cannot prevent program to have bugs, exploits or errors before releasing it. Because it is
-
00:28:05 almost impossible for a single person to notice all of the bugs and fix them before
-
00:28:11 releasing them. I have fixed hundreds of bugs, errors, maybe thousands during the 11 years,
-
00:28:19 all by the logging system that I have developed. And from the player base, the user space
-
00:28:28 feedback. Therefore, converting an application is a huge task and a huge waste of time. So choose
-
00:28:40 intelligently before starting your application. Therefore, in this
-
00:28:47 course, I am teaching the latest technology. ASP.NET 5 MVC 5 is the latest version and
-
00:28:55 Bootstrap 5 is the latest version. Therefore, we are very up to date and we are going to
-
00:29:02 learn the latest technology. I wish in my school I had learned it that way, but
-
00:29:08 we didn't. If you already know how to use Web Forms, you should definitely give ASP.NET MVC a
-
00:29:15 try, especially if some of the above mentioned problems have been bugging you as well.
-
00:29:21 If you're new to web development, and you need to decide between the two technologies, I would
-
00:29:25 still recommend giving ASP.NET MVC a try. The MVC model can seem a bit restrictive to some people,
-
00:29:33 and having to follow a pattern is obviously harder in the beginning than not following one,
-
00:29:38 but once you get used to it, it's very pleasant to work with and judging from the amount of attention
-
00:29:42 that the MVC model receives in general, it's not likely to disappear anytime soon.
-
00:29:49 OK, so let's continue the tutorial. We have seen what is about MVC and what is about .NET Core. OK,
-
00:29:56 so we need Visual Studio Community Edition. As I said, it is installed, latest version,
-
00:30:01 latest updates are done and we will be using.NET Core and ASP.NET Core 5
-
00:30:07 version 5. This is a very neat version and previous year, for example, this wasn't available.
-
00:30:18 OK. If you have worked with programming languages or frameworks from Microsoft before,
-
00:30:25 you probably already know Visual Studio. It's an IDE (Integrated Development Environment) perfectly
-
00:30:32 suited for working with .NET technologies like ASP.NET MVC and programming languages like C# and
-
00:30:39 VB.NET. An IDE is an advanced editor, where you can usually edit your code but also manage your
-
00:30:45 projects, compile your code and so on. OK, here another thing. If you don't know programming,
-
00:30:52 don't worry. In our YouTube channel, which is SEcourses, I have three playlists. OK,
-
00:31:05 so the first playlist you should start with is Introduction to Programming with C Sharp
-
00:31:10 Full Course. In this course, you will learn how to do programming with C Sharp from beginning,
-
00:31:18 from scratch. Then Advanced Programming with C Sharp, which I have just started
-
00:31:25 uploading the videos. So as per week, each week I will upload another video. OK, then
-
00:31:35 finally there is the third course, which is also completed the previous semester is
-
00:31:43 Object Oriented Programming with C Sharp Full Course. When you watch these
-
00:31:50 course lectures, you will have great experience and knowledge about C Sharp programming on in
-
00:31:58 programming languages, how to do programming. Then it will be much easier for you to
-
00:32:05 follow our ASP.NET and MVC.NET Core with Bootstrap course. OK, let's continue.
-
00:32:15 Yes, so we need Visual Studio. You can download it from visualstudio.com
-
00:32:22 and there is also Mac OS version as well. I think there is also Linux version as well, but I'm not
-
00:32:30 sure you can search for it. OK, let's start our application. So we will start with. Let's see.
-
00:32:46 Empty. Yes, empty. OK, so you see this tutorial is based on ASP.NET Core version 3.1, which was
-
00:32:54 available previous year. But this year there is newer version, so we will continue with newer one.
-
00:33:02 So we are starting with an empty project here, ASP.NET Core project. And I think we also
-
00:33:11 check the HTTPS. OK, we don't need this HTTPS. No need it. OK, and.
-
00:33:28 OK, let's continue. Great.
-
00:33:32 So our project is being composed right now. You see it is in C Sharp, Linux,
-
00:33:40 Mac OS, Windows, Cloud, Service Web. It works almost every platform that you can imagine.
-
00:33:48 Oh, there is new update for my Visual Studio. I should do it. Probably it came today.
-
00:33:56 OK, so our project is composed. So you see we have program, CS program class is almost empty
-
00:34:06 and we have startup class. It's also almost empty. So in the startup class,
-
00:34:16 we have routing. We have endpoints. And which will write to the screen as "hello world".
-
00:34:27 OK, let's continue with the tutorial.
-
00:34:40 OK, so.
-
00:34:44 Let's change it as they writing. OK, so, and then we have settings as Json file. And we have launch
-
00:34:55 settings. So you see they are automatically generated. IISExpress is used. And in startup.
-
00:35:06 OK, so what does this means that this application will just return this response to the screen
-
00:35:15 when we go to the root of the our ASP.NET core web application. Root means that there is no
-
00:35:22 pages, just the root URL. When the application starts, you will see. So this is the root URL
-
00:35:30 of our localhost application. You can imagine it as www.google.com and there is no slash.
-
00:35:40 OK, so we are seeing the message, the response.
-
00:35:44 The response is written like as await context.Response.WriteAsync("hello MVC world").
-
00:35:53 So the context is HTTP context. Response is the response and WriteAsync is the asynchronous.
-
00:36:02 OK, so this method gets called by the runtime. Use this method to configure the HTTP request
-
00:36:08 pipeline. So in this configuration, we will configure our application start
-
00:36:13 and other things we will see as we progress in the
-
00:36:20 tutorial and in our course. So right now I am running in debug mode. Currently, any CPU,
-
00:36:29 which means that my application is running as 32 bits. OK, we can also set it as
-
00:36:37 64-bit which can increase our speed. Let's make it. I will click new and pick 64.
-
00:36:48 OK, now my application is 64 bit. To be able to use this, of course, your Windows
-
00:36:58 has to be in 64-bit as well. It should work I think. OK, let's
-
00:37:07 continue to next. We are still in the getting started section by the way. OK, it is working
-
00:37:15 as expected. We will stop the application. OK, now we will start with composing a controller.
-
00:37:22 What was about controller? If you remember. Let's read it and remember it. In the last article,
-
00:37:31 we created our very first ASP.NET MVC project. It had built-in functionality to display a simple
-
00:37:38 message to the world, but we obviously want more than that! The first thing that would
-
00:37:43 be relevant to add to your new, almost empty MVC (Model-View-Controller) project is a Controller.
-
00:37:51 As we talked briefly about earlier, the Controller acts as the middleman - it
-
00:37:55 will combine your Model with a View and serve the result to the end-user.
-
00:38:00 However, neither a Model nor a View is required - the Controller can act on its own for the most
-
00:38:05 basic operations, e.g. delivering a simple text message or redirecting the user to somewhere else.
-
00:38:12 By the way, I want to mention another positive feature of MVC model. You see, in big projects,
-
00:38:23 there are groups of developers developing different layers, such as a group of developers
-
00:38:30 develops the background code, a group of developers develops the user interface,
-
00:38:36 another one develops the controllers. So you see, MVC model lets you two
-
00:38:43 have different teams totally independently working from each other. If you are using web forms,
-
00:38:50 then it is harder to separate teams as easy as in ASP.NET MVC core, ASP.NET
-
00:39:00 MVC. Because with MVC pattern, you are able to separate development into the very distinct
-
00:39:08 layers and it is much easier to have different teams for each layer. So this is another great
-
00:39:17 advantage of MVC pattern development.
-
00:39:24 However, there are a few things we need to do before adding a new controller to our project.
-
00:39:29 Adding MVC support to a Web project. Ok, so by default our application is not supporting
-
00:39:36 MVC pattern. So first we will add the MVC pattern functionality to our project. In
-
00:39:44 the previous article, we created a new web project using an empty template.
-
00:39:49 This leaves us with a very basic web application, so we need to add MVC
-
00:39:54 support to it, to let the .NET framework and the web server know how to process
-
00:39:58 incoming requests etc. Remember the Startup.cs file which we modified in the previous article?
-
00:40:05 It's time to open it up again in Visual Studio and look for the ConfigureServices method.
-
00:40:11 It's currently empty, but let's change that by adding the following line to it:
-
00:40:16 So here you see we have the ConfigureServices method which is being called at the startup of
-
00:40:23 the application as from the name you can see. So in ASP.NET Core MVC, the startup.cs and program.cs
-
00:40:34 are extremely important. Their names is extremely important because it is how the application gets
-
00:40:40 started. So we add services, services.AddMVC() method call to our
-
00:40:48 application. So you see services is already coming from our framework. Ok.
-
00:40:59 So yes we have made it, we also need to let's read it.
-
00:41:05 We also need to modify the Configure() method - previously, it contained some code to output
-
00:41:10 our "Hello, MVC world!" message, but we want our new Controller to handle this task in the future.
-
00:41:17 Your web application needs to know how to map incoming requests to your controllers
-
00:41:21 and for this, it uses the concept of routes. We will talk a lot more about routing later on,
-
00:41:27 because it's a slightly complex subject, but for now, we will use a couple of lines of code,
-
00:41:32 found at the bottom of the below code example, to add default routing to the application.
-
00:41:38 So, modify your Configure() method in the Startup.cs file so that it looks like this: Ok,
-
00:41:45 so it is about how to get the pages, how does the engine resolve the URL path into the which page
-
00:42:02 it is. Ok, so I will just copy and paste. So what is changed? The changed thing is actually only
-
00:42:12 app.UseEndpoints. Ok, here let me copy-paste it. So we are removing this app endpoints.MapGet
-
00:42:22 to map default controller root. Ok, so this is a method call from endpoints and
-
00:42:29 endpoints is from Microsoft ASP.NET Core Routing iEndpointRouteBuilder and we call this method. So
-
00:42:38 if you wonder what this method is doing, you see in the description, adds endpoints for controller
-
00:42:45 actions to the Microsoft ASP.NET Core Routing iEndpointRouteBuilder and adds the default route.
-
00:42:52 So the default route is something like this. You see controller home, action index, and ID.
-
00:43:00 Ok, let's continue. Now we are finally ready to add our first controller. We
-
00:43:04 haven't added a controller yet. We are still at the setup. Adding a controller:
-
00:43:14 The project structure of an MVC application is up to you - you can place all the controllers,
-
00:43:19 models, views and other files in the root if you want to.
-
00:43:24 However, it's usually a good idea to split things up in relevant folders, e.g. a "Controllers"
-
00:43:29 folder for your controllers, a "Views" folder for your views and so on. Once you get used to
-
00:43:35 a certain structure, whether it's a common one or one you have made up your self, you will find
-
00:43:40 it a lot easier to navigate your project and work with the files. Also putting these files into the
-
00:43:47 these way named folders makes your job easier because by default .NET's framework looks
-
00:43:55 for certain paths under certain folders. Therefore, you should follow this pattern.
-
00:44:02 So lets add a controllers folder to our project. I click the lecture 1, right click and I click add
-
00:44:11 folder, controllers. Ok, it is done. Then we will add our first controller as add new item and MVC
-
00:44:30 controller empty. Ok. We will name it as home controller. By the way, this naming is important
-
00:44:37 because by default the ASP.NET core will look for home controller. If you name it differently,
-
00:44:43 it won't work. I will show you that. So I right-click the folder and
-
00:44:50 you see controllers, right-click add controller.
-
00:44:56 So MVC controller empty, edit.
-
00:45:02 I will name it as HomeController.cs. You see it is already named like that by default. Ok, add it.
-
00:45:10 Ok, home controller is added. So you see now it calls, it has a method, public method as
-
00:45:18 returning type is IActionResult and the name is Index. And it return a View(). Ok.
-
00:45:25 So View is also a method of controller class. Ok, so it is added.
-
00:45:39 As you can see, it looks like a regular C# class. It inherits the Controller class, which is how the
-
00:45:46 .NET frameworks know that this class is to be treated as an MVC Controller. It has one method
-
00:45:52 called Index(), which will try to return the default view by calling the View() method. The
-
00:45:58 .NET framework will be able to find the proper view, if it exists, in one of the many places
-
00:46:03 that it automatically looks for views. However, we have not reached the part of the tutorial
-
00:46:09 where views are introduced, so let's instead change the method to return the, now famous,
-
00:46:14 "Hello, MVC world!" message. Change the single line of the method so that it now looks like this:
-
00:46:21 Ok, so since we didn't add any views to our MVC application yet,
-
00:46:28 we are commenting this out and we are returning a content instead of a view. Ok.
-
00:46:36 And let's continue. So now, our startup will route to default controller and the default
-
00:46:45 controller will check the home controller and home controller by default will check the index.
-
00:46:54 So what does that mean? That means that when we open our application, the Index
-
00:47:02 action will be called in the default URL. Ok, let's see it.
-
00:47:17 Ok, it is working as expected and we can also type like this, I think, but not sure. Oh,
-
00:47:24 can't yet. So, since we didn't add the proper routing, it only works like this so far. Ok.
-
00:47:42 So this is working due to default routing
-
00:47:47 and if I change the controller name, I think it won't work. Let's try.
-
00:48:01 Anyway, I won't. So I didn't.
-
00:48:09 Ok, I have to change this as well.
-
00:48:14 Ok, now when we run the application, it should throw an error, I think, because
-
00:48:21 this is not one of the default controller names.
-
00:48:31 Ok, so you see, it can't be found.
-
00:48:36 So the default naming is important if we are using default routing.
-
00:49:02 Ok, now it should work. So you see this is under ecture_1.controllers namespace.
-
00:49:17 Meanwhile, let's continue to our project.
-
00:49:28 Ok, so now it is time to compose a View. If you have read the previous articles, you should now
-
00:49:36 have a very basic ASP.NET MVC project, capable of outputting a simple "Hello world" message.
-
00:49:43 This text is generated directly in the controller and returned as plain text to the browser,
-
00:49:48 but obviously that won't be practical for anything other than the most rudimentary tasks.
-
00:49:54 What we want is of course dynamic pages, created with HTML and other web technologies.
-
00:50:00 For that, we need Views, which are the visual representation of the Model returned
-
00:50:05 by the Controller. Ok, this is important. Let's repeat it. For that, we need Views,
-
00:50:10 which are the visual representation of the Model returned by the Controller. Since
-
00:50:18 we have already created a Controller (called HomeController), we're now
-
00:50:21 at the point where we should create a View for it, instead of just returning a piece of text.
-
00:50:27 And as we saw in the previous article, Controllers usually live in a folder called "Controllers",
-
00:50:32 so we should have a folder called "Views" as well.
-
00:50:36 Just right click the project in the Solution Explorer and select Add -> New Folder,
-
00:50:40 just like we did in the previous article. It's also customary to have one folder per Controller,
-
00:50:46 so inside the new Views folder, let's create a folder called Home, to contain the view(s) we have
-
00:50:52 for our HomeController. With that in place, we are now ready to create a View inside our new folder:
-
00:50:58 Ok, it is a good practice to follow the common patterns that have been
-
00:51:07 developing by the community, therefore I will also follow that pattern. So let's add views folder.
-
00:51:20 Views. Then for each view, I will compose another, for each controller,
-
00:51:34 it is also customary to have one folder per controller inside the views folder. Yes, for each
-
00:51:40 controller we should have a folder and therefore I will also add another folder inside this as
-
00:51:52 home. Ok, so this will be for home controller.
-
00:51:59 Then inside it, I will add index view. Ok, so let's make it. Let's read this first.
-
00:52:07 You will see a dialog with quite a bit of options. They are all very relevant and we will discuss
-
00:52:12 most of them later in this tutorial, but for now, let's just add a simple View to our project.
-
00:52:18 You can do that by replicating the options I have used from this screenshot:
-
00:52:23 Ok, so let's add our index view, which will be our index page. So I will pick Razor View - Empty
-
00:52:34 and when I click add, I think there will be additional
-
00:52:43 options. So it is adding index.cshtml. Is it true?
-
00:52:56 Yes, we are adding Razor view, not a Razor page. It is important. Let's add it. Ok,
-
00:53:05 looks like the other options are removed in the version 5. Therefore, we didn't get these options.
-
00:53:21 Ok, we have a,
-
00:53:24 we have i think more emptier page, but it should be same index cshtml. So let's copy and paste
-
00:53:39 this into our view. So we have layout set as null and we have some HTML code. This is HTML code if
-
00:53:47 you know. Document type HTML, HTML head. Head is the part where we put metadata about our page.
-
00:53:59 It is not directly displayed to the user and in the body tag, we will write our code.
-
00:54:12 That's basically just some standard HTML for a blank document, with only a little bit MVC-related
-
00:54:18 code in the top, based on the Razor syntax, which we'll get to in one of the upcoming chapters.
-
00:54:24 For now, just ignore it and work with the HTML, which you hopefully already know and understand.
-
00:54:36 At this point, let's make a small modification to the HTML, to display a greeting to the world - it
-
00:54:42 should of course go between the tags, like this: This is simple HTML code. You see there is a span
-
00:54:51 and inside span we are setting the font size to 18 pixels. We are writing hello, but MVC will be bold
-
00:55:01 and world. Anything else? Okay, now we will return view inside our home controller here.
-
00:55:13 I will comment this and I will return this. Okay, so, now it will return this index.cshtml.
-
00:55:25 But how does it know to return index? Because since this is home controller, it will check
-
00:55:34 first views folder by default, then it will check home folder by default. Then inside it,
-
00:55:41 it will look index.cshtml because we are in the index action result, as you can see
-
00:55:50 here. So it will match this and this and home and home here and views folder. These are all
-
00:55:58 default paths and naming. Now let's run it and see whether it is working or not.
-
00:56:09 Now we are able to, as you can see, format our returned data to the users with HTML.
-
00:56:22 This is a Razor page, therefore it is cshtml.
-
00:56:29 Okay, now you see the page is titled as index. When we inspect,
-
00:56:34 we can see it inside the head tag. You meta name="viewport", content="width=device-width",
-
00:56:42 and title is index and inside the body our span is printed like this. It is working as expected.
-
00:56:53 If I change it to this and refresh, it won't work
-
00:56:59 because it requires me to restart the application. However, there is a solution for that. I believe
-
00:57:05 we will see that as well. It requires me to restart the application for the changes
-
00:57:13 to reflect. Okay, let's just continue, then we will get that part.
-
00:57:22 Okay, so how does it work? Let's read it as well. Thanks to the default routing mechanisms found in
-
00:57:30 the ASP.NET MVC framework, the root URL is automatically routed to the HomeController's
-
00:57:36 Index() method (don't worry about routes just yet, we will talk about them soon).
-
00:57:41 With a call to the View() method, a number of locations are searched to find a View
-
00:57:45 with a matching name, in this case [project root]\Views[name of controller]\Index.cshtml.
-
00:57:52 This view is then interpreted (as it may very well contain Razor code) and then returned as output to
-
00:57:58 the browser. So as I explained it, this is about default routing mechanism implemented to the .NET
-
00:58:07 Core MVC. Therefore, since we have followed the naming pattern, it is able to find
-
00:58:16 the default pages. However, you can also give them custom names, it is possible of course,
-
00:58:22 but for now, we will continue with this. So the summary, let's
-
00:58:28 read it. We have now successfully combined a Controller and a View to generate an actual web
-
00:58:34 page. It might still seem like voodoo or black magic, but just move on in this tutorial and
-
00:58:40 get a feeling of how it works and how powerful the framework is, before trying to fully grasp
-
00:58:45 all the concepts. There's a lot more you need to know about Views though, to take full advantage of
-
00:58:50 ASP.NET MVC - this was just an introduction, to get you up and running. In an upcoming chapter,
-
00:58:57 we'll dig much deeper into the subject of Views. Okay, we are still in getting started section,
-
00:59:04 so let's go to the next article. Okay, creating a model.
-
00:59:13 Okay, so time to compose a model. In the last two articles, we started out by creating a
-
00:59:22 Controller and then a View. We combined the two, to create a simple, HTML based web page.
-
00:59:29 However, at this point, our View might as well have been a flat HTML file, because it didn't
-
00:59:35 do anything other than output basic HTML. The idea behind MVC (Model-View-Controller) is of
-
00:59:42 course to mix HTML with data generated by the server and this where the Model comes into play.
-
00:59:51 In the MVC architecture, the Model is generated by the Controller and then passed to the View,
-
00:59:56 which outputs the relevant data to the user. As you saw in the previous article,
-
01:00:01 we can do without the Model if we don't have any server-generated data for the specific page,
-
01:00:06 but as soon as we do, we'll use a Model. If you are not going to use any server-generated data,
-
01:00:15 then it is best to use static HTML, not having heavier and complexer programming language. So,
-
01:00:27 therefore, in the today's applications,
-
01:00:31 almost in all cases, you will need a server-generated data, therefore a model.
-
01:00:39 But what does a Model look like? Well that's actually up to you, because a Model can be any
-
01:00:44 kind of object found in the framework. It could in fact be a simple number or string, or it could be
-
01:00:50 a complex object instantiated from a class, e.g. a User class which holds information about a user,
-
01:00:56 a GuestbookEntry item which contains a post to a guestbook or anything else.
-
01:01:02 That also means that your Model can be a class you already have, e.g. something that comes from the
-
01:01:07 database, or a class that you create specifically to become a Model for one or several Views.
-
01:01:13 Okay, time to add a model. Let's add a Model to our HelloMVCWorld project
-
01:01:20 (created in the previous article on controllers). For the sake of this example,
-
01:01:25 we will be creating a new Model instead of relying something already defined in the project.
-
01:01:32 Just like we usually keep controllers in a folder called "Controllers" and Views in a
-
01:01:37 folder called "Views", we will add a folder called "Models" to the root of our project.
-
01:01:43 So, right-click the project name in the Solution Explorer and select Add -> New Folder, just like
-
01:01:48 we did in the previous articles. When you have added the "Models" folder, it's time to create
-
01:01:54 the actual Model. As mentioned, models are just regular classes, so that's what we'll be adding:
-
01:02:01 Okay, so let's compose our directory with add new folder models. Inside that model,
-
01:02:14 we will add a model named as movie. Okay, so our example will be movie.
-
01:02:22 So, you see we are giving a custom name to our model.
-
01:02:27 It doesn't have to be a standard name. So, from here, I will click new item
-
01:02:36 and I think we will add it as a regular class. Yes, regular class. So, I will just add,
-
01:02:48 class and name it as movie. Okay, movie.cs is added. You see it is public class movie.
-
01:02:59 Okay, and. Since we will use this class to contain basic information about a Movie,
-
01:03:08 in the dialog that pops up simply enter Movie.cs as the name of the new class.
-
01:03:14 Visual Studio will create a new, empty class for you, which should look like this: Okay,
-
01:03:19 so you see here, HelloMVCWorld.Models, it is their namespace. This is the namespace
-
01:03:26 that is defined when we compose our project. Our namespace is lecture_1, as you can see.
-
01:03:33 And then we will add 2 public properties.
-
01:03:42 String Title and DateTime ReleaseDate. These are properties, not fields, if you wonder what are
-
01:03:48 the difference between fields and properties, you can watch my Advanced Programming course lectures.
-
01:04:00 Now our Model class is ready. As we talked about, the Model should be instantiated
-
01:04:05 by the Controller, so let's head over to the HomeController we created in a previous article.
-
01:04:11 It previously just returned a View from the Index() method, like this: Okay, so this is what
-
01:04:16 we have currently and now we will modify it into this. So, let's go to our HomeController, I will
-
01:04:24 comment this and paste this one. So, what this is going to do is, so you see under models namespace,
-
01:04:36 I will compose an instance of movie here. I make the definition here and I
-
01:04:44 initialize it here with new keyword. This is regular C sharp syntax, nothing different.
-
01:04:51 I set the title and I set the release date and I return again view, but inside that view I return
-
01:05:02 an object. You can only return single model to your view.
-
01:05:10 Therefore, if you want to return multiple classes, then you need to expand your movie class into a
-
01:05:17 parent of multiple classes and then you would be returning multiple classes in a one return.
-
01:05:25 So, you cannot write here a chain of different objects or such.
-
01:05:31 View can only have empty, you see, I'm currently showing the method overloads of view method.
-
01:05:41 It can return object model, currently, what we are doing, it can return a string view name,
-
01:05:47 name of the view. By default, it will be index view because it is under index method and then
-
01:05:57 it can return the model and index view name. So, it will return movie right now
-
01:06:05 and our object model, but we are not done yet. Let's continue. We did two changes:
-
01:06:13 We instantiated a new Movie object, with one of most critically acclaimed movies in
-
01:06:18 the world and then we passed this object to the View() method. This will make sure that
-
01:06:23 our model is available in our View. Remember the View we created in the previous article,
-
01:06:28 called Index.cshtml? Time to open it up again and have a look. Okay, this is an important sentence.
-
01:06:38 Let's read it again. This will make sure that our model is available in our View. available in our
-
01:06:43 view. So, to access our model in our view, we have to return it. Okay, we have to first initialize
-
01:06:51 it and then we have to return it. Because this is an instance-based class. As previously mentioned,
-
01:07:02 a View can work without a Model just fine, but when we want to actually use a Model,
-
01:07:06 we need to make the View aware of this and tell it which type we expect the Model to be.
-
01:07:12 This is done with the Razor @model directive, usually in the top of the View file, like this:
-
01:07:18 Okay, so our Razor page cannot just know what will it get. Therefore, we define our model like
-
01:07:29 this with model keyword here. This is a special keyword. It starts with @ character. Then you see
-
01:07:36 currently it is giving me an error because my namespace is different. This is lecture_1 and
-
01:07:42 inside lecture_1 namespace under models class or folder rather than not class but folder. I
-
01:07:52 link it to the movie class. Okay, so this is the model of this Razor page right now. You
-
01:07:59 cannot define two models, three models or more models. But you can define one or no models.
-
01:08:07 I mean, you cannot define more than one model inside a single Razor page.
-
01:08:14 We have to define this because it is a runtime thing.
-
01:08:17 And when it returns at the runtime, it is the way it knows the model. Okay.
-
01:08:30 Now our View knows that it should expect a model of the type Movie, which gives you
-
01:08:34 at least two advantages: If a wrong type of model is accidentally passed to the View, an error will
-
01:08:39 be raised and also, Visual Studio will be able to help you with IntelliSense when you use the model
-
01:08:45 in the view. So let's do that: Okay, so this is a really important part. Now that we have defined
-
01:08:51 it, IntelliSense will help us to use it. So in the title, I will use the title property of the
-
01:09:03 model with @Model.Title.
-
01:09:10 So now you see I am able to access model object inside this view with
-
01:09:19 @model keyword. And it's public parameters, public properties or fields with that name.
-
01:09:26 So using how clean code it is, it's extremely clean, well-structured, easy to understand, and
-
01:09:33 easy to write. This is combination of server-side coding and HTML coding. Right now what we are
-
01:09:41 doing. And with an extremely clean and structured way. Writing a clean code and easy-to-understand
-
01:09:50 code is extremely important part of being a good software engineer. And then we will change the
-
01:10:01 printed message like this. The movie @model.title, I am able to use it as I want. It was released
-
01:10:12 @model.ReleaseDate.ToLongDateString(). This is a method extension of datetime.
-
01:10:28 Yes, it's a struct. I remember it correctly. And I am calling to long date string
-
01:10:35 extension of my release date. So let's run the application and see what it prints on the screen.
-
01:10:49 Meanwhile, it's running let's read it.
-
01:10:52 Notice how I reference the keyword Model several times in the markup now - this gives me direct
-
01:10:57 access to the model we passed into the View, in this case the instance of the Movie class.
-
01:11:03 That means that I can use properties from it, e.g. the Title and the ReleaseDate properties.
-
01:11:08 Time to test our work - press F5 to run the project and hopefully you should be met
-
01:11:13 with the information about "The Godfather". Okay, this is also important. You may also
-
01:11:18 have noticed that I prefix the Model keyword with the @ (at) character, e.g. @Model.Title.
-
01:11:24 This is all part of the Razor syntax, which we'll talk much more about in an upcoming chapter.
-
01:11:29 Okay, let's read it. Okay, now our title is The Godfather and the
-
01:11:35 printed messages is The movie The Godfather was released Friday, March 14, 1972.
-
01:11:41 So this is the long date in my system culture, which is English US. I can also change it to,
-
01:11:53 I think, Turkish culture. Let's see if it does support. No, it doesn't support it.
-
01:12:00 And so I can also format this as I want, such as, let's see. Let me show the span, style
-
01:12:10 equal to font size, 25 pixels and color is
-
01:12:20 something. And then I will add the span here. By the way, you should not do this inline styling.
-
01:12:29 It is not the preferred way, but just for right now, I am showing. We will use proper CSS and
-
01:12:39 other formatting. Let's just use this way first. Then I will show you the way of CSS formatting.
-
01:12:56 Okay, now it is styled as I wanted. Okay, let's continue to our tutorial.
-
01:13:07 Okay, so we have passed the chapter introduction, the chapter getting started. Now we are in the
-
01:13:20 Razor chapter. We will start Razor chapter.
-
01:13:25 It has been over one hour. So let's write so far what we have
-
01:13:36 seen. Lecture 1 context. Ok. Let's copy and paste from here. Okay, and let's continue.
-
01:14:48 These are important things to know. Okay, and then
-
01:15:01 this was an important part of the lecture. Okay. You know, don't need this.
-
01:15:41 Okay. Ok
-
01:16:14 edit is a better word.
-
01:16:45 The Razor page. Yeah, I think you can call it that way.
-
01:17:03 Okay, I think this is Razor view. Yeah. Okay, this is what we have seen so far. Use it.
-
01:17:24 Okay. So let's continue with introduction to Razor pages.
-
01:17:35 So we are under the Razor category chapter and it starts with introduction. When
-
01:17:45 Microsoft first created the ASP.NET MVC framework, it used WebForms pages to display content.
-
01:17:52 However, WebForms wasn't as flexible as people were used to from other MVC
-
01:17:57 frameworks - it had too much overhead in the form of ViewState, server controls etc. Therefore,
-
01:18:04 Microsoft decided to implement a much simpler and more lightweight language/view
-
01:18:08 engine for the MVC framework. They called it Razor and it was released in January 2011,
-
01:18:14 as a part of ASP.NET MVC version 3. So you see how late the Razor's engine were implemented
-
01:18:24 in MVC framework. It is done in 2011 with the version 3. Now we are in the version 5.
-
01:18:35 Razor allows you to write in various "dialects", based on your favorite .NET language.
-
01:18:40 In this tutorial, we will be focusing on the C# version of Razor,
-
01:18:44 but you can use Razor with VB.NET as well. I will check something first versions. Okay.
-
01:19:02 Yeah, we are inside the MVC version 5.2 with a .NET core version 5. Okay.
-
01:19:11 But may there be other versions? Let's check the Wikipedia.
-
01:19:21 Let me see. Okay, we are in the core.
-
01:19:25 Oh, in the core, we are still at MVC version 3.1.1. But if this is this page up to date.
-
01:19:42 Okay, this is .NET. We are in the .NET core.
-
01:19:56 Oh, say they have used different versioning numbers.
-
01:20:08 Yeah, now it is .NET 5, .NET core. Therefore, this is not the versioning anymore. Okay,
-
01:20:15 let's continue. All right. Why use Razor? Why use Razor?
-
01:20:24 The biggest advantage of the Razor is the fact that you can mix client-side markup
-
01:20:29 (HTML) with server-side code (e.g C# or VB.NET), without having to explicitly jump in and out of
-
01:20:36 the two syntax types. For instance, consider this example of a page in ASP.NET WebForms:
-
01:20:43 Okay, so this is what I was talking. You can mix client-side markup with a very awesome
-
01:20:50 clearness. So this is an example from ASP.NET web forms. You had to use these markup tags each
-
01:21:00 time you were using a server-side object, server-side feature, and it was really
-
01:21:12 confusing. I will compose a web form project to show you how messy it was.
-
01:21:24 We won't be using this project, but I just want to illustrate you how it was being
-
01:21:32 displayed. So I am going to pick ASP.NET core web application again. Okay, it can be.
-
01:21:44 I will pick a random folder. Documents, perhaps. Okay.
-
01:21:52 And then I will pick .NET framework. .NET core web application, not model-view-controller. Oh,
-
01:22:04 this is not a web form. This is still razor pages. Sorry, I will compose another project.
-
01:22:20 Let's compose a new project. I think for web forms I have to pick another thing.
-
01:22:36 Okay, let's just remove, close this project. Okay, here web form when I type.
-
01:22:51 Okay, it is called as ASP.NET web application. This is web forms. Next.
-
01:22:59 Okay, you see I am able to pick the .NET framework version. It is latest is .NET framework version
-
01:23:06 4.8. I am not sure they may have ended the development. So web forms don't need HTTPS. Okay.
-
01:23:17 So this won't be an empty project. Therefore, I will be able to show you the difference.
-
01:23:48 Okay, meanwhile, let's continue. So this was the syntax of web forms. We had to use these opening
-
01:23:57 tags and closing tags wherever we want to access server-side controls. And it was really making
-
01:24:04 it messy. Believe me. Okay, here there are some pages. Let's see whether they have used it or not.
-
01:24:13 You see at the top you had to use some definitions like this.
-
01:24:19 Okay, there isn't any. Okay, here, for example, for the tag accessing title parameter from the
-
01:24:27 page that title, you had to open it and close it like this everywhere. Everywhere you are using.
-
01:24:35 Okay, so I can cut this and paste it here. It would work
-
01:24:41 like this. And in the Razor, we just write it like this. So I will put a new line.
-
01:24:53 Here. You see, I'm able to access name object, name model. Or.
-
01:25:06 Yeah, name model and the current date like this. However, we don't have a name
-
01:25:11 definition right now. Maybe we can define it in the
-
01:25:18 code somewhere. Yeah, it is defined like this. So you see currently we don't have something as name.
-
01:25:43 I will open a server tag here like this and I will define a string. As name equal to.
-
01:26:03 Now it should become available. Yes, now it's available. As we can see, it is so clean.
-
01:26:10 And easy to understand. You see the difference.
-
01:26:15 Okay. And now I can run the application. So this is the difference. Let's continue.
-
01:26:28 Now obviously this basic example doesn't save you a ton of keystrokes, but in the long run,
-
01:26:33 this makes it a lot easier and much faster to build your pages and combine markup with code.
-
01:26:39 This becomes more obvious when you want to do something slightly more advanced,
-
01:26:43 like making a conditional statement in your view. Okay, yes, it is so true on the conditional
-
01:26:49 statements. It is much harder. For example, you see, I open it, I close it the if then I type it,
-
01:26:56 I open, I close again, then I open and I close again. It becomes much more messier
-
01:27:03 as you write a more complex code. I know from my web-based game development. However,
-
01:27:09 in Razor, it is much cleaner and easier like this. So you see inside the server tag opening,
-
01:27:19 we are still able to write HTML code with HTML markup. Currently, it doesn't.
-
01:27:28 It tells me that I don't have requests in my Razor view. I think to be able to have requests,
-
01:27:40 I have to define it in my server-side tag as
-
01:27:53 it is probably inside. Request, perhaps. Or maybe we can just directly access it like this.
-
01:28:29 Something like this.
-
01:28:45 It tells that you cannot access it like this. Interesting. Let's find a proper example of this.
-
01:30:19 Yes, it is.
-
01:30:27 If it is ambiguous or perhaps type of string values, I don't know.
-
01:30:55 Maybe this.
-
01:31:25 It is the string values.
-
01:31:53 OK, I think you can use it like this and then Request.query.
-
01:32:05 OK, so let's run it and test it. However, I wonder if this will throw an exception or not,
-
01:32:14 since we are not providing a query as test query parameter.
-
01:32:24 So you see some examples of this tutorial might be outdated or whatever the tutorial you find on the
-
01:32:34 Internet. Therefore, you may be needed to spend some time to find it, to find the correct syntax.
-
01:32:46 OK, now I am waiting for my application to start.
-
01:33:16 This may have a built-in null check. I'm not sure. Yeah, this has a built-in null check. Therefore,
-
01:33:25 it is null right now. Oh, it is not null. So what is the value? So I have to check it as not equal
-
01:33:36 to, rather than not equal to, I have to check as length bigger than zero. So this should be better.
-
01:33:45 OK, so this will not be parsed by default right now because the length should be zero.
-
01:33:58 I think this has a built-in null handling. It doesn't show the errors. Yeah. OK, so you see
-
01:34:12 it didn't enter inside this if because the length is zero. However, I can now add test equal to.
-
01:34:23 Let's write awesome test. OK, now we can see it. You see, lots of markup here,
-
01:34:30 test values, awesome test, and even more here. When I change it to something else,
-
01:34:35 it won't work because it will be expecting. So you see how clear the code is.
-
01:34:42 And I can also say that from my experience, .NET Core is better handling at null values. OK,
-
01:34:52 so it takes your it makes your job even easier than regular .NET framework.
-
01:35:02 OK, now let's continue to next. Let's see our duration. OK, it is already one and half hour.
-
01:35:20 OK, so. Just write first page.
-
01:36:00 So basic Razor syntax, let's read it.
-
01:36:07 In this chapter, we will look at all the basic principles of the Razor syntax.
-
01:36:12 The most basic thing you can do with the Razor syntax is to access something from
-
01:36:16 the server-side by prefixing a variable or function name with an @ (at-character). We
-
01:36:22 saw a couple of examples of this in the previous chapter, where we can simply mix
-
01:36:26 HTML, text and server-side variables all together like this: Yes, we have seen it.
-
01:36:35 The above expression outputs text, mixed with the name variable (has to be declared elsewhere) and
-
01:36:40 then it access the current date from the Now property of the DateTime struct, and thanks to
-
01:36:45 the simple syntax, the code is actually quite easy to read. This is true. By the way, we are
-
01:36:50 able to access DateTime because it's a struct. Therefore, you don't have to define it anywhere.
-
01:36:57 It is a framework. Let's say framework feature. However, for name parameter,
-
01:37:05 I have to define it elsewhere, which I do in the code here. OK.
-
01:37:14 So let's continue. But the @-prefix is not just for accessing simple variables or properties
-
01:37:24 on a class. It's used for pretty much anything in Razor, including inline control statements,
-
01:37:30 code blocks, comments and much more, as you will see in the following examples.
-
01:37:35 So the key character of Razor pages is @. It was this one in the web forms. OK.
-
01:37:46 <% and %>. And let's continue HTML encoding. This is important, really important.
-
01:37:59 You should be aware that the when you use the implicit Razor expression syntax shown above,
-
01:38:04 the output will be automatically HTML encoded. This is usually the behavior you want,
-
01:38:10 but in some cases, you want to be able to output HTML and have it interpreted by the
-
01:38:15 browser instead of rendered as output. For this, you can use the Raw() method on the Html class
-
01:38:21 and here's an example where you can see the difference: OK, so this is a good
-
01:38:26 thing to show. If you don't encode your output, that would make your application vulnerable to XSS
-
01:38:36 attacks. In our lecture, I have explained it with details in our Security of Informations
-
01:38:51 course. Here you can find its playlist. Security of Information Systems. If you watch these
-
01:39:02 lectures, you will understand what I mean. XSS attacks, cross-site scripting attacks is very
-
01:39:09 dangerous. Therefore, the encoding is important. If you are displaying a client input on another
-
01:39:19 client's screen, you should encode it properly or you have to be sure that it is sanitized.
-
01:39:27 Sanitizing is not the best protection or it must be a controlled input. So, here the "hello world".
-
01:39:39 As you can see, it has HTML tags. It could have script tags as well.
-
01:39:47 And the first one will print it as like this exactly, and the second one will show it as
-
01:39:55 bold and will not show the HTML tags. Let me run the application.
-
01:40:01 I will show also the script version as well. By the way, this is a great playlist
-
01:40:16 series. You should watch the Security of Information. Actually,
-
01:40:19 you should get that course in your last year. If you are not a student of Toros University,
-
01:40:30 you can watch it. So the first one is encoded, therefore it is written as a text, no matter what
-
01:40:40 the hacker puts inside that message, it will be
-
01:40:44 printed as a text. So let me show what I mean. I am adding a script
-
01:40:55 like this. This could be a malicious script as well, which would hack your computer.
-
01:41:03 Alert, you are hacked. So you will see the difference. In the first case, it will be printed
-
01:41:11 as raw tag. It will be printed as a script, string. However, the HTML raw will print it as
-
01:41:20 exactly as it is an HTML code. And we will see the alert message, when the page is rendered.
-
01:41:30 You see you are hacked. And the other one is scripted as a string.
-
01:41:35 So this is what is about HTML raw and by default encoding.
-
01:42:02 Okay, or let's say how input is encoded. This is a better definition.
-
01:42:12 Let's continue. Yeah, we have seen it. Explicit expressions:
-
01:42:23 In the first example above, we used a so-called implicit expression, but some situations calls
-
01:42:28 for a more explicit variant, to show the parser exactly what your intentions are.
-
01:42:33 Razor comes with an explicit expression syntax for just those situations and
-
01:42:38 basically it's all about wrapping your expression with a set of parentheses.
-
01:42:42 This makes it easier for the parser to understand what you're doing, and allows
-
01:42:46 for stuff like calculations and modifications inside of a Razor expression. Here's an example:
-
01:42:58 Okay, so we have a name parameter. We already have it here. I won't
-
01:43:04 define it again. So name substring first from beginning zero index and four characters. And then
-
01:43:14 it will print your age and like this. This was impossible in web forms, writing code like this.
-
01:43:25 You had to use all opening and closing server-side parsing. Notice the syntax, where the @-character
-
01:43:40 is followed by a set of surrounding parentheses. We use it two times, first to access the Substring
-
01:43:46 method on the string variable, and then to do a piece of very simple math directly inside
-
01:43:51 the Razor expression. The resulting output looks like this Hello, John. Your age is: 42. Okay, so
-
01:43:59 in this case, I will just get the six characters, my first name. I will type my true age is okay.
-
01:44:31 Okay, so my age is displayed. My name is displayed as well. Actually, I am about to be 34.
-
01:44:42 Let's write something like this, plus five, plus three, plus four, plus five.
-
01:45:02 So I can write anything inside these parentheses and it will understand that expression.
-
01:45:12 And let's continue. This is explicit expressions.
-
01:45:32 Multi-statement Razor blocks: If you need to do more than simply accessing e.g. a variable,
-
01:45:39 Razor allows you to enter a dedicated, multiline code-block by entering a start curly-bracket
-
01:45:45 after the @-operator. Here's an example: This is also another great feature of
-
01:45:55 razor pages, razor syntax. So you see inside here, I am defining whatever I want and then
-
01:46:02 I can access it like this simply. So we make some math operation, we define some string
-
01:46:10 and then we compose some string like this. Then I can write encoded
-
01:46:18 text variable like this. So let's run the application to see. By the way, when you
-
01:46:27 are watching this lecture, please just don't copy-paste my code. Write it yourself. Because
-
01:46:35 with writing yourself, you will understand it much better. So you see text hello world, the result is
-
01:46:44 42. Then we didn't put any new line. Therefore, it is written like this. Hello world, the result
-
01:46:50 is 42. Hello world, the result is 42. I could also write something like this. Let me show you.
-
01:47:00 So here I would make like this. The result is sum plus i and not n. n is not a valid HTML.
-
01:47:17 Let's say new line, you have to type it like this. However, this won't be printed
-
01:47:26 as a new line because we are using encoded.
-
01:47:30 So let me show you what I mean. Currently, it will be printed as encoded. Therefore, we will
-
01:47:39 see it as a br tag. It is like this because it is encoded. Then I will convert it to HTML raw.
-
01:48:02 Like this. Now you will see the difference. OK.
-
01:48:29 Now it is printed as I wanted with new lines increasing each time with
-
01:48:36 one and such. I could write anything I want. OK, and let's continue.
-
01:48:50 Notice how I can write regular C# code inside the code-block, including loops,
-
01:48:55 conditional statements and everything else that you're used to from the C# language. One
-
01:49:00 important difference though is that you have to have curly-braces around your control statements
-
01:49:05 inside of code blocks, even if they only span one line - otherwise you will confuse the parser.
-
01:49:11 Also notice how I can define a variable inside the code block,
-
01:49:14 modify it if needed, and then use it outside of the code block!
-
01:49:21 OK, so this is a multi-statement razor blocks.
-
01:49:34 OK, and now this is another great feature. HTML tags and plain text inside code blocks:
-
01:49:47 There were no such feature in web forms. This is a razor feature. So, when you're inside a
-
01:49:54 code block, like shown in the example above, you may still need to output text - in fact,
-
01:49:59 that's quite normal. But instead of forcing you in and out of code blocks, Razor allows
-
01:50:04 you to mix in HTML tags directly in your code blocks, like this: This is an awesome feature.
-
01:50:12 Let's test it. So you see I am inside the code block. However, I am able to write the known HTML
-
01:50:24 tags as well, such as p or it could be a div as well. So you see it's automatically
-
01:50:33 different colored. By the way, it says that it is assigned, but never used.
-
01:50:43 And I can simply use it like this as well. Let me show you.
-
01:50:52 Like here. So this will be printed. And this is defined here and this is a markup having
-
01:51:00 text. It is awesome. Believe me, it is awesome. It is much cleaner and easier code.
-
01:51:08 Razor syntax is much better than the traditional ASP.NET Webform syntax.
-
01:51:21 Razor simply sees your HTML tags and then assumes that you're now giving it markup for
-
01:51:26 output instead of code for processing. But what if you don't want to wrap your text inside of tags?
-
01:51:33 Razor has an option for doing this as well, using the @: operator:
-
01:51:43 So you see it is printed. This is a tag with plain text and markup inside it is a code
-
01:51:48 block. OK, it is working and then we can use it as well. What
-
01:51:59 does this mean is this is plain text. OK, this is plain text inside
-
01:52:10 code block because you cannot write the plain text inside code block. This is invalid
-
01:52:18 because it is expecting a variable. When I run it, I will get an error. You will see.
-
01:52:28 OK, so let's comment it out for commenting. Let's use. You cannot type plain text inside code block.
-
01:52:50 So how can I write? I can write a plain text like this. This is the single-line plain text.
-
01:52:56 And then I can also write multi-line plain text like this with covering it text
-
01:53:04 block. Multiple lines. This will break it because it is just only covering single line.
-
01:53:27 OK, this is not getting commented out. OK, so you see if I am going to comment
-
01:53:35 inside this part, I have to use this. Otherwise, it is not working. OK. All right.
-
01:53:50 When we run it we will see it. By the way, if you do multiple lines like this, I think it won't
-
01:53:58 parse it as multiple lines. But let's check that as well. Probably we need to put
-
01:54:04 br tag. New line tag. It is still a single line and prevent it. We can put
-
01:54:19 br tag like this. It should work, I think. Let's try it. You see, it's automatically understanding
-
01:54:25 that you are using an HTML tag inside text and parses it that way. It is awesome. Let's see it.
-
01:54:40 Yes, now it's multi-line. OK, and let's continue. Razor server-side comments:
-
01:54:54 Sometimes you may want to leave comments in your code, or comment out lines of
-
01:54:58 code temporarily to test things. Therefore, most programming languages include a syntax
-
01:55:04 for commenting your code and so does Razor. You could just use HTML comments, but as we all know,
-
01:55:10 these are rendered to the browser and can therefore be seen if the "View source"
-
01:55:14 option is used. Razor comments on the other hand are never rendered to the browser but simply
-
01:55:20 ignored when the page is parsed. Here's an example on how you can have a Razor comment in your views:
-
01:55:27 So I think this is also not shown, but not sure. Let's check it from the source.
-
01:55:37 Yes, this is also rendered as a server-side comment, so it is not parsed
-
01:55:47 in the browser. So this is also another server-side comment.
-
01:55:51 Inside Razor, you can use regular comment system as well. OK, let's write them like this.
-
01:56:05 And like this. Oh, we are still inside of a server tag, so I will write it like this. OK,
-
01:56:17 so we have seen the basic Razor syntax as well. I will write it as a comment here.
-
01:56:29 OK, so you see it is an invalid way of comments right now because we are not inside
-
01:56:36 a server tag. Therefore, I will comment it out.
-
01:56:48 Like this. OK, I think for today it is enough. Please do everything I did with the same order.
-
01:56:59 Meanwhile, watching you can pause it. If you have any questions, best way is contacting me through
-
01:57:08 our Discord channel. I will show that right now. So these are Discord channel link. You can click
-
01:57:17 and join it. If you didn't join the from previous semester, you see we have already 70 members.
-
01:57:26 And after you have joined it, currently I will be using browser,
-
01:57:29 but you can use Discord application as well. Meanwhile, let me write another thing.
-
01:57:50 OK, you have to pick a role. Currently, there is a general channel which is available to everyone
-
01:57:59 and you should read the kurallar-rules
-
01:58:06 channel for selecting a course. Which is this course currently. I will just click 8.
-
01:58:18 Ok it didn't work why?
-
01:58:31 This is not working. OK, we will fix it, but it is not a problem
-
01:58:36 when I click this. OK, none of them works.
-
01:58:43 Maybe my account is broken.
-
01:58:54 I will check and fix this. But you should join our
-
01:59:02 Discord channel. I will be leaving. This is my dummy account, not real account.
-
01:59:09 And let's upload our code to our GitHub repository. So let's check why. You should also
-
01:59:19 learn how to use a code management system such as GitHub and Git Bash. OK, basically Git itself.
-
01:59:30 So it says that you have lecture one, but it is not added yet. So let's add. Then let's write
-
01:59:38 our commit name. So lecture one final code. And let's push it to our GitHub repository.
-
01:59:55 OK, when I refresh it, it should arrive.
-
01:59:59 You see lecture one has arrived already. However, I will make a name change. So let's. Make it like.
-
02:00:09 Lectures. Put this inside lectures folder. OK, so now it is changed, as you can see. So I will add.
-
02:00:27 So it will move object from lecture one into the lectures folder.
-
02:00:36 It's updated. With code management, you can see every version. So there are six commits. And
-
02:00:43 when I click this, you can see that version. So this is the previous version. You see,
-
02:00:49 it is like this. I can fully see it. And this is the latest version. So this is the awesomeness of
-
02:00:56 code management. I can see every version of the repository like this. OK, and I think this is
-
02:01:05 enough for today. End of lecture. Hopefully, see you next week. Feel free to ask me any questions.
