Тёмный

Upgrading to .NET Core: From WebAPI to ASP.NET Core API - A TimCo Retail Manager video 

IAmTimCorey
Подписаться 422 тыс.
Просмотров 32 тыс.
50% 1

Next up in the .NET Core upgrade is our WebAPI. We will upgrade our API to .NET Core 3.0. Part of this process will include using the new dependency injection system. We will also replace our dependencies and work through authentication.
** TimCo source code now at: www.iamtimcore...
Full Courses: www.iamtimcore...
Mailing List: signup.iamtimc...
One-off tutorials are awesome but they aren't the only thing you should be doing to learn C#. Another vital part of learning is learning how to put it all together. This interactive course is all about putting the pieces together. You can watch each video on its own or you can watch them in order and see a bigger picture. The choice is yours.
This course focuses on real-world development. As such, we are simulating that we work for TimCo Enterprise Solutions on a brand new product, the TimCo Retail Manager. Just like in the real world, we are starting out with one set of requirements but know that over time they will change.

Опубликовано:

 

11 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 69   
@quentincouissinier8533
@quentincouissinier8533 3 года назад
Don't have the words to express how much I love you work.
@IAmTimCorey
@IAmTimCorey 3 года назад
I am glad you find it valuable.
@Stoney_Eagle
@Stoney_Eagle 4 года назад
Now I'm getting in somewhat familiar terrirory again, feels much relaxing to follow along now.
@IAmTimCorey
@IAmTimCorey 4 года назад
Good to hear!
@emascochukwujindu5528
@emascochukwujindu5528 4 года назад
Another amazing episode. Thanks @IAmTimCorey for being awesome!
@IAmTimCorey
@IAmTimCorey 4 года назад
Thank you!
@erikvolger5631
@erikvolger5631 3 года назад
The new project options keep changing in Visual Studio. Now 18 months later I didn't have the same project type so I used the one that seemed to be similar (ASP .NET Core Web App) but I didn't get any controllers, just pages. Adding folders for the Controllers and Models wasn't difficult and creating the new classes was the same. It all worked in the end. I got confused when the default roles needed to be created because I had no HomeController. I added it to the privacy.cshtml.cs file and that works also. So for everyone having the same problem, this is an option.
@IAmTimCorey
@IAmTimCorey 3 года назад
Yeah, that is a struggle. They seem to redo that menu annually at least, which makes it hard to follow along at first with projects that aren’t brand new. I am glad you figured it out.
@bradbernhardt750
@bradbernhardt750 2 года назад
Erik, could you elaborate a bit more as to what you did to get the controllers working? I have the same issue that you ran into.
@erikvolger5631
@erikvolger5631 2 года назад
@@bradbernhardt750 That was a few months ago and in the mean time started a new job where I don't use C# ASP .NET I Don't know what I did to make it work. I have to review the video and look at my code. Give me a day or two, and meanwhile you keep trying to make it work... Good luck!
@bradbernhardt750
@bradbernhardt750 2 года назад
@@erikvolger5631 Thanks for the quick response... I did figure it out. I initially chose the .NET Core Web App but not the one with MVC which I needed.
@miniputlol
@miniputlol 2 года назад
Wanted to add that you could've chosen ASP.NET Core Web App (Model-View-Controller) project type instead - it already has those folders baked in.
@milankroupa275
@milankroupa275 5 лет назад
Hi Tim. Really nice tutorial. Will need this very soon. Thanks
@IAmTimCorey
@IAmTimCorey 5 лет назад
You are most welcome. Thanks for watching.
@ludovicwagner2656
@ludovicwagner2656 3 года назад
Thanks Tim for your tutorials. I come across the "An error occurred while sending the request". I don't think I missed any step but will see it I get it solved with the next tutorials :-)
@IAmTimCorey
@IAmTimCorey 3 года назад
Stick with it. I'm sure you will get it.
@alexandrunuta4284
@alexandrunuta4284 4 года назад
for anyone using password with different validation (I use one char password since this is a tutorial) first, for the back-end logic, you need to go to Startup.cs > ConfigureServices method and in the "services.AddDefaultIdentity( options =>...)" add the password options like here docs.microsoft.com/en-us/aspnet/core/security/authentication/identity-configuration?view=aspnetcore-3.1#password for the front-end, the views have their own validation as well, but the viewmodels need to be scaffolded (like it says in the video at 12:25) docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-3.1&tabs=visual-studio#scaffold-identity-into-an-mvc-project-with-authorization do as above and select Register, SetPassword (ChangePassword and ResetPassword in case you want to use them later from the razor pages) Now expand folders Areas>Identity>Account>Manager; the scaffolded pages can be expanded (have an arrow next to the plus sign) and *.cshtml.cs is shown; there you can clearly see where you need to edit the minimum password length Hope this helps
@IAmTimCorey
@IAmTimCorey 4 года назад
Thanks for sharing.
@pilotboba
@pilotboba 4 года назад
Copying and pasting to new files is fine for a project with 5 controllers. But, migrating a production project this way is going to take a long time.
@IAmTimCorey
@IAmTimCorey 4 года назад
It is, but the other option is to make manual changes to each file.
@pylvr8021
@pylvr8021 5 лет назад
He tim amazing tutorial as always, can we expect one about Xamarin soon ?
@IAmTimCorey
@IAmTimCorey 5 лет назад
Soonish but yet.
@JohnPeter-yf5jf
@JohnPeter-yf5jf 4 года назад
I recommend implementing those over rides, first it's the only way to make a model that nherits from ApplicationUser (If you wanted add to the default model) Also those pages still work when they are in the DLL; and Most are active. Don't like the idea of active pages responding without them being in the project.
@IAmTimCorey
@IAmTimCorey 4 года назад
Thanks for the suggestions.
@orisit9038
@orisit9038 2 года назад
Hi Tim i'm having a little problem about the new Api project and any kind of help would be greatly appreciated. I have this 'No connection could be made because the target machine actively refused it' error when i start the app. i tried to change the base address Uri in App.config in my Wpf UI which the new API uses (localhost:44300) but still got the error, and i tried to put a breakpoint on the base address from the ApiHelper which i hard coded the Uri of which the new Api uses, but it still doesn't change the previous uri (localhost:44321). Im stuck here trying all the possible solutions and i can't move forward, what should i do? Thank you.
@IAmTimCorey
@IAmTimCorey 2 года назад
Check your API URL by loading it manually in the browser. Make sure it is correct. Then work from there step-by-step to find the problem.
@orisit9038
@orisit9038 2 года назад
@@IAmTimCorey Thanks for the response Tim. I have it working when i updated my UI to .NET Core from your video, it just fixed itself automatically, i guess its a bug on .net framework or something, but Thank you for all of your videos and efforts, it really helped me a lot as a "career shifter" hire in my current job. God bless you more and your family. Thank you.
@Freeliner75
@Freeliner75 4 года назад
Adding roles like that (45:10) could be an excellent opportunity to show how a seed migration can be done. First you have to override the ApplicationDBContext like that: protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); builder.Entity().HasData( new IdentityRole { Id = Guid.NewGuid().ToString(), Name = "Admin" }, new IdentityRole { Id = Guid.NewGuid().ToString(), Name = "Cashier" }, new IdentityRole { Id = Guid.NewGuid().ToString(), Name = "Manager" } ); } Then, go to the Package Manager Console, type Add-Migration SeedRoleData. Then type Update-Database And voila, the role data is in a database already.
@IAmTimCorey
@IAmTimCorey 4 года назад
I'm not usually a fan of seeding migrations. At least not directly. It feels too locked in. I usually pull the info from the appsettings.json for the initial admin user and then allow them to go from there.
@alexandrunuta4284
@alexandrunuta4284 4 года назад
Hi, My kestrel console doesn't log any info except the few lines when starting the app I tried changing the logging level to diagnostic, added Logging to services in startup.cs, added logging configuration in Program.cs > CreateDefaultBuilder, added console template in appsettings.json, but nothing worked, the kestrel console didn't log anything (info or error). Did I miss something in the video? Thank you
@IAmTimCorey
@IAmTimCorey 4 года назад
They made an update to trim back what is displayed. Check your appsettings.developer.json file to make changes to the log level so you can see more info.
@alexandrunuta4284
@alexandrunuta4284 4 года назад
​@@IAmTimCorey That was it. I think I understand now, they're both part of the list of environments the IConfiguration looks through when trying to find a setting. And I suppose multiple environments can be created, each used in specific stages, similar to application-*.properties files used by spring boot for java Thanks!
@matiascatanzariti1195
@matiascatanzariti1195 4 года назад
@@alexandrunuta4284 Could you please share your solution for the Kestrel issue? I haven't been able to find the way to see the detailed info that Tim shows in the video.
@Stoney_Eagle
@Stoney_Eagle 4 года назад
@@matiascatanzariti1195 Change: "Microsoft": "Warning", to "Microsoft": "Information", in your appsettings.Development.json
@ameral-mahdawi2742
@ameral-mahdawi2742 4 года назад
Hi Tim, Thank you for these amazing series. I have three questions though: 1. You mentioned from the start of this project that you are not a big fan of EF, I am not a fan of it as well as I like to have full control of the connection to the DB. But you used it in the WebAPI part for Identity. I've searched the web for a way to use Identity and bypass EF. I did find some complex solutions for that by creating ApplicationUser (inherits from IdentityUser) and ApplicationRole (inherits from IdentityRole) models, and create UserStore and RoleStore classes that implements some interfaces in order to load and save data to the DB. Do you have a simple way of doing so or you recommend using EF for Identity? 2. Instead of having two databases, why don't we merge them into one DB? 3. Instead of having the register/login actions in the same WebAPI project, why not moving them out into the UI project? Best regards Amer
@IAmTimCorey
@IAmTimCorey 4 года назад
1. There is not an easy way to separate EF from Identity so I use it. 2. If you merge the two databases, you have security information in with your normal information. This is not a good security practice. 3. The register/login actions are in the UI - the API is the UI of that application. The WPF project is a totally separate project. You can use the API without using the WPF project.
@abdallabenomran6809
@abdallabenomran6809 5 лет назад
Thanks for the video Tim . really like always very cool . just a question what about C# Data Access course i thought it will be released at the end of Okt.
@IAmTimCorey
@IAmTimCorey 5 лет назад
That was the goal. However, I had a rather busy October and could not complete it. My goal is to have it out by the end of this month and the other two out by the end of December.
@erikvolger5631
@erikvolger5631 3 года назад
Don't make the same mistake I made! I accidentally added a SECOND constructor to a Controller for DI. Then the DI system cannot instantiate and your endpoints are not working... The error I got from swagger helped me a lot, so always use swagger or postman to try out endpoints before blaming the code. Before that I had a strange issue with json (AuthenticatedUser properties in PascalCase didn't work but camelCase did...) that is now also solved somehow.
@IAmTimCorey
@IAmTimCorey 3 года назад
Thanks for sharing.
@Guacamole42
@Guacamole42 5 лет назад
Great video, love your teachings, clear and awesome. I had a question Tim. I came upon a class with a constructor with this keyword in it. I looked it up and seems classes can have this keyword in constructor but I fail to see why you would use this, it isn't clear to me. Maybe one day I will see an answer in one of your videos?
@IAmTimCorey
@IAmTimCorey 5 лет назад
I'm assuming you are talking about the "this" keyword. "this" refers to the current instance of the class and usually can be done without (it is assumed).
@LarryB15
@LarryB15 3 года назад
Hi Tim. Probably a silly question, but swagger does not work on this version. Is that expected?
@IAmTimCorey
@IAmTimCorey 3 года назад
We will get it to work again.
@StefanJarina
@StefanJarina 4 года назад
Hi Tim, did you ever had issues with the default developer ssl certificate for ASP.NET Core on Windows? I have certificate trusted (via dotnet dev-certs https --trust), chrome still see the site as "Not Secure" and in WPF at the end of this video I don't have "Not Found" but error: "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel." and then inner error: "The remote certificate is invalid according to the validation procedure."... No idea if this is in app and will be fixed later, or really an issue on my system.
@IAmTimCorey
@IAmTimCorey 4 года назад
Nope, never had an issue. It sounds like a local issue. Maybe see if you can reset it?
@Freeliner75
@Freeliner75 4 года назад
Hi Stefan, not sure if that is your case exactly, but I found out that there is a known bug still lingering in .Net Core for WIndows 8.1 users. When you run Kestrel it could not establish secure Http2 connection, and you have to force it to use Http1 instead. You can see the description and discussion here: github.com/dotnet/aspnetcore/issues/14350 The solution is shown here. Just make sure to put Http1 instead of Http2 in an example. github.com/dotnet/aspnetcore/blob/0a26524af0121e9db4ad1f3a6f98e33c3b52cdbd/src/ProjectTemplates/Web.ProjectTemplates/content/GrpcService-CSharp/appsettings.json#L9-L13
@1mposs1b1e9
@1mposs1b1e9 4 года назад
@IAmTimCorey can you also use ASP.NET Core 2.1 For this application? Is it much different than ASP.NET Core 3.1?
@IAmTimCorey
@IAmTimCorey 4 года назад
You could for the API but not for the WPF project. It is different, though. I would recommend going to 3.1.
@rykeeconsulting
@rykeeconsulting 4 года назад
Hi Tim. Have an issue have been chasing for awhile, when logging in this "string userId = User.FindFirstValue(ClaimTypes.NameIdentifier);" is returning a null userId.(endpoint on Core API) Any idea why this might occur. Seems I am getting a token when logging in, but this is null.
@IAmTimCorey
@IAmTimCorey 4 года назад
Does the endpoint allow anonymous connections? If it does, it won't capture the logged in user's info. Make sure the endpoint is locked down and try it again. It should work then.
@rykeeconsulting
@rykeeconsulting 4 года назад
@@IAmTimCorey I have locked down the endpoint. For some reason "User.FindFirstValue(..." is null, have added using statement "System.Security.Claims" before contacting you. Have added Swagger and tested in Postman. When I manually add userId with code below things work in WPF. When I request token and use token in Postman it returns correct info. Recreated project from Git on another machine, get same error. Am going through video's to see if something is configured incorrectly. Do you have any idea where I might look? string userId = User.FindFirstValue(ClaimTypes.NameIdentifier); // below code manually gets proper userId, above code is null not sure why if (userId == null) { userId = "f35d8a18-31fe-4ab9-9218-f24a30389eda"; } Have enjoyed the course, find it very informative.
@rykeeconsulting
@rykeeconsulting 4 года назад
Found my error Tim. Was in the TokenController.cs file. When creating the Claim I added this line "new Claim(ClaimTypes.Name, username)," twice, when I needed to add this line "new Claim(ClaimTypes.NameIdentifier, user.Id)," for the Id. was going through all the "ClaimTypes", tried to access some and realized they have to manually be added to the "GenerateToken" method. Was a real good exercise in JWT Authentication, debugging, essential of Visual Studio 2019, darn cutting and pasting. "Ce la vie" to several hours. Thanks for a great course.
@sovannseung6985
@sovannseung6985 4 года назад
Thank you for the great video. I do have a question regarding authentication. In this video, when you create ASP.NET Core project, you choose "Web Application" and for Authentication you choose "Individual User Accounts" then EF will create some ASpNetxxx tables in SQL Server for Authentication. Is there any other way for authentication if I don't want to use EF at all in the project? I want to fully use Dapper only. Do you have any video about that?
@IAmTimCorey
@IAmTimCorey 4 года назад
There are other authentication and authorization systems out there but I don't know of any that use Dapper (and I don't create my own).
@sovannseung6985
@sovannseung6985 4 года назад
@@IAmTimCorey Thank you for your answer.
@Programmernawaz
@Programmernawaz 4 года назад
Hii Tim you doing greate jop. I am a aps.net developer,i have some knowledge mvc or api, but I want more learn mvc and Rest web api, but I am confused that with whom do i learn it asp.net or asp.net core and which asp.net core version Right
@IAmTimCorey
@IAmTimCorey 4 года назад
I have a course on choosing which ASP.NET Core project type to use: www.udemy.com/course/which-aspnet-core/?couponCode=JULY2020 As for .NET Framework vs Core, I recommend .NET Core for new projects but to learn both since you will probably get a job working with .NET Framework.
@jonwhitehead2102
@jonwhitehead2102 4 года назад
Could you have used a web application template with Razor Pages for this also instead of MVC?
@IAmTimCorey
@IAmTimCorey 4 года назад
I didn't use MVC, I used API. That's a different type of web project that just sends and receives data. No, I couldn't have used a Razor Pages project instead.
@jonwhitehead2102
@jonwhitehead2102 4 года назад
@@IAmTimCorey I'm sorry. I meant in the new template you chose "Web Application (Model-View-Controller)" I think because of the identity where it was in the application vs an Azure store (around 7:54 mark). I just didn't know if it's possible to use the razor pages template and then just add in the controllers for the API's
@IAmTimCorey
@IAmTimCorey 4 года назад
Ohhh, sorry, my bad. I was confused. Yep, I chose MVC because that is what Identity uses natively for the pages. You could have chosen Razor Pages (it still would have used MVC for the identity part) and then added API.
@jonwhitehead2102
@jonwhitehead2102 4 года назад
@@IAmTimCorey Thank you sir! I'm trying to build the API and have a small front-end to manage users of it in the same project. I have most of it going and think you're lessons helped me pull this together! I also signed on as a patreon!
@bradbernhardt750
@bradbernhardt750 2 года назад
I've been following along these videos for awhile and took a month break. When I went back to load where I left off (half-way through this video), Visual Studio won't load the TRMData database. 'S:\...\TRMData.dbmdl could not be created. Internal Error: Ese could not be initialized.; Invalid API parameter'. If anyone has seen and resolved this error before and can help me out, I'd be most appreciative.
@IAmTimCorey
@IAmTimCorey 2 года назад
Could this be the solution: dev.emptywolf.com/2014/10/dbmdl-could-not-be-created-internal.html
@bradbernhardt750
@bradbernhardt750 2 года назад
@@IAmTimCorey Thanks for the link. That article and many others I have tried haven't worked so I'll just redo the database stuff in all the lessons. Thanks again. Update: Just tried creating a new Database project from scratch and am getting the same error. Must be an issue with my Visual Studio.
@SuperDre74
@SuperDre74 4 года назад
Why not immediately add the default roles when the API is starting, and you can add roles to a default user.. I added it now (mostly based on a stackoverflow question/answer and added the loading the information from the appsettings.json) I only add the default roles now if they don't exist (so I can easily extend the appsettings.json with new ones (ofcourse I could have hardcoded the list)). As I mentioned earlier, I don't like the "let's see where the ship get's stranded and hack a way around it" approach of how these tutorials seem to go lately. But the information will get you further along (but if it's the right way to go about converting an app to .Core?, well it is a way to do it like this, just let's leave it at that (like how the controller all still use the old hardcoded Route[xx] with every single function instead of using base and extending it). But looking forward to seeing how the problems with the webapi are gonna be solved.
@IAmTimCorey
@IAmTimCorey 4 года назад
We will get there on the default user/roles. It isn't a day one requirement. As for the "hack a way around it" approach, I have found that this is actually the most efficient way to do it in the real world. Sure, in tutorials you can make it all pretty and neat but the real world is messy. This application is a simulation of what you will find in the real world. By approaching the upgrade this way, we identify the issues specific to our application rather than going through a pre-formatted list that doesn't apply to our situation. We also improve our debugging skills.
@coffeeimp4451
@coffeeimp4451 2 месяца назад
In Visual Studio Community 2022 for me when building was getting errors using .AddRoles() in the startup.cs. I had to change it to use this instead: services.AddIdentity(options => options.SignIn.RequireConfirmedAccount = true) .AddEntityFrameworkStores(); So change from services.AddDefaultIdentity to services.AddIdentity and pass in Identity Role there.
Далее
Satisfying DIY Earing for the Little Ones! 😲
00:33
Managing User Roles - A TimCo Retail Manager Video
1:12:42
The Problem With Microservices
17:47
Просмотров 435 тыс.