For the last few days, I’ve started learning ASP.NET Core. After the initial research on how to build the UWP project for a client of mine, I think my idea of using ASP.NET Core Identity could actually work for this project. Since I don’t know how to use ASP.NET Core yet, I think I want to get comfortable with using it with a Web UI first before I go and build just an API using it. I’m currently watching a Pluralsight course about it by Scott Allen, I’ll be writing down what I found different on ASP.NET Core compared to ASP.NET MVC to make the idea stick.
The framework needs a matching name in the parameter of the C# method, and in an input inside the Razor View. There are different ways to get data from a request in ASP.NET core. One is using the HttpContext, and the other is model binding. Using the HttpContext for model binding is not considered the best approach according to Scott.
When you make an http request (invoke a method from the PageModel/Controller), the framework’s model binder looks in the posted form values, query string, and http headers. You can even make your own custom model binder. The goal of model binding is to move info from the request into an input model.
When the parameter to the method called by the framework (OnGet for example) does not contain anything, ASP.NET Core will default to null if the parameter is a reference type, and throw an exception if the parameter is a value type.
These tag helpers are new to me – I don’t remember it existed before ASP.NET Core – and they remind me of those tags used by AngularJS. The three that I encountered in the course are asp-page, asp-for, and asp-route-
The asp-page tag helper sets an anchor tag to point to a Razor page. On the other hand, the asp-for tag helper is used to bind a C# property to the html. This tag helper works on inputs and labels. Note that you don’t need to type Model.
First, it sets the html input’s name property, the model binder will try to match this name to a C# property once it checks the query string, route data, etc. when you make an http request – input model. Second, it fills the html input’s value when the server populates the page (responds to the request) with values from the Page Model property – output model. The Input model and Output model are two new helpful terms I got from Scott here.
Last, we have asp-route-
That’s it for this post. Now I’ll continue the course and will likely create a new post about what I learned.