How to Publish an ASP .NET Core 6 Website / APP on a Windows OS and Fix Database Connection Problem #323
FurkanGozukara
announced in
Tutorials
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
How to Publish an ASP .NET Core 6 Website / APP on a Windows OS and Fix Database Connection Problem
Full tutorial: https://www.youtube.com/watch?v=9xoIEbVdiZc
You can watch the video with manually written English subtitles.
I recorded this video upon the request of one of my follower students. His ASP .NET Core 6 Web APP was not working when he published it. However, the app was working fine in development / debug mode.
He was developing his app using #ASP .NET Core version 6 #MVC #CSharp (C#) and MSSQL server express edition.
He has used Migrations to generate his database with code first approach as explained here: https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/?tabs=vs
After doing some debugging I found out that his app was not working when published due to database authorization being denied.
In this video, you will learn how to publish and run your ASP .NET Core 3/5/6/7 (any version) app on a Windows Operating system. This OS can be Windows 8, 10, 11, or Windows Server as well.
First, you need to install the IIS server through Turn Windows Features on or off and then install the necessary ASP .NET Core Runtime Hosting Bundle: https://dotnet.microsoft.com/en-us/download/dotnet/6.0
The source of the example application used in this video can be downloaded here: https://github.com/FurkanGozukara/how-to-publish-ASP-.NET-Core-MVC-application
So if your published ASP .NET Core application/website is not working this video will likely solve your problem. Moreover, if you are not sure how to publish your developed app and run it on a remote host or on a local host and emulate it as a remote domain, this video will teach you both.
To solve the database connection problem, we generate a new SQL Server Login account with the necessary permissions. Then we modify SQL connection settings in appsettings.json file. After that when publishing the APP we modify the SQL connection string as well.
Please make comments regarding the video and give me your feedback. You can also ask new questions and I will try my best to answer them.
If you are interested in programming but you don't know where to start, I suggest you watch our playlists in the following order: https://www.youtube.com/c/SECourses/playlists
[1] Introduction to Programming Full Course with C# playlist
[2] Advanced #Programming with C# Full Course Playlist
[3] Object Oriented Programming Full Course with C# playlist
[4] Asp.NET Core V5 - MVC Pattern - Bootstrap V5 - Responsive Web #Programming with C# Full Course Playlist
[5] Artificial Intelligence (AI) and Machine Learning (ML) Full Course with C# Examples playlist
[6] Software Engineering Full Course playlist
[7] Security of Information Systems Full Course playlist
[8] (Turkish) Bilgisayar Becerileri Tam Ders playlist
Video Transcription
00:00:01 Hello everyone.
00:00:02 Welcome to my channel again.
00:00:03 This is Dr. Furkan Gözükara.
00:00:05 I am going to answer a question of a mine a follower student.
00:00:10 He was developing a .NET Core 6 MVC application.
00:00:13 ASP .NET MVC application and his application were not working when he published his application
00:00:22 a Windows Operating system.
00:00:25 I had to debug his application and find out the reason because when I run his application
00:00:31 it is just running fine on a local host in a debug mode.
00:00:37 As you can see like it is a simple application.
00:00:39 But when we have published it in a Windows 10 operating system it was not working.
00:00:46 So, first of all to publish an ASP .NET Core MVC application in a Windows Operating system
00:00:54 you have to install a turn windows features on or off and you
00:01:07 need to install .NET ASP.NET 4.8 and Internet Information Services.
00:01:16 IIS.
00:01:17 So an ASP.NET application runs on an IIS Server in Windows Operating system.
00:01:23 Either it can be Windows 10 or it can be Windows Server.
00:01:28 It doesn't matter.
00:01:30 After you have installed these as you can see Internet Information services and ASP.NET
00:01:35 4.8 you also need to install ASP.NET Core Run Time Hosting Bundle.
00:01:45 You see there are packages for Windows, macOS, Linux and your target operating system.
00:01:53 For Windows 10 and since we are using ASP.NET Core 6 in this application we install this
00:02:01 one.
00:02:02 Windows Hosting Bundle.
00:02:04 I have installed both of them.
00:02:06 So the application is currently ready to publish.
00:02:13 So this student of me has used ASP.NET Core actually entity framework .NET Core with migrations.
00:02:24 You see it is here Migrations Overview if you are interested in it.
00:02:28 This is code first then database approach.
00:02:33 It uses it generates a migrations folder under your application as can be seen here.
00:02:40 By the way I will upload all of the source code to the GitHub and I will also put the
00:02:48 links I display on here about Migrations and .NET Core Run Time.
00:02:57 Ok.
00:02:59 So this migrations command that you run through let me show you command line, developer command
00:03:07 prompt or developer powershell.
00:03:10 It generates these necessary code in here and by using these you see these auto generated.
00:03:20 And it generates your database based on your code.
00:03:25 Ok.
00:03:26 So these are the code that is written by my student follower student and I am showing
00:03:33 you how to make it run through this application.
00:03:40 So let me show you it will generate a ToDoApp database when I run the application first
00:03:49 time.
00:03:51 Lets run the application.
00:03:54 It is running.
00:03:55 It is initializing.
00:03:56 And lets see.
00:04:00 Ok.
00:04:01 It is here as you can see.
00:04:03 So when we publish it why do you think it is not working.
00:04:10 Ok.
00:04:13 We need to reconnect I think.
00:04:16 This is weird.
00:04:18 Maybe it is because I don't have the necessary rights.
00:04:26 Ok.
00:04:28 Lets just do something.
00:04:33 The application is working.
00:04:37 However, when we publish this application it won't work.
00:04:43 By the way I am annoyed with this problem.
00:04:45 So I will just disconnect and reconnect with Windows authentication.
00:04:55 Ye.
00:04:57 Ok.
00:04:58 Actually I was in a another user probably.
00:05:02 As you can see our database is working.
00:05:04 There is migrations history.
00:05:06 This is used to update the database if you change code.
00:05:10 Alright.
00:05:11 So now lets publish our application and see if it is working or not.
00:05:17 This was why student problem.
00:05:20 He uses default settings as you can see the connection string is written in appsettings.json
00:05:28 file.
00:05:29 It is written as server localhost, database todoapp, integrated security true.
00:05:34 This works fine when you run in debug mode.
00:05:37 However, when we publish it what will happen.
00:05:41 This is the problematic thing.
00:05:44 I am opening IIS server.
00:05:47 I am going to compose a test website.
00:05:51 Ok.
00:05:52 I am going to add a website.
00:05:53 I will call it as todoapp.
00:05:57 Ok.
00:05:58 Let's generate a folder for it in our inetpub folder here.
00:06:05 wwwroot new folder todoapp.
00:06:07 Ok.
00:06:08 And I am going to call it as todoapp.com.
00:06:12 There is no such there could be such host but we will use our localhost.
00:06:18 todoapp.com.
00:06:20 And.
00:06:22 Ok.
00:06:24 Inetpub wwwroot todoapp.
00:06:30 Ok.
00:06:32 Ok so to be able to access todoapp.com through my localhost I need to open and edit hosts
00:06:41 file.
00:06:42 It is inside Windows ok drivers actually system32 yes folder.
00:06:51 Here.
00:06:52 And here is drivers folder.
00:06:54 Here etc folder and hosts file I will open it with EM editor and I will add 127.0.0.1.
00:07:02 This is localhost IP and now it will redirect me to localhost IP when I type todoapp.com
00:07:14 in my browser.
00:07:16 Ok.
00:07:17 So we are ready.
00:07:18 And now it is time to publish our application.
00:07:22 I am going to use publish and lets see the options.
00:07:27 There is release .NET 6 self contained or framework depended.
00:07:31 It is up to you.
00:07:33 You can also select the target operating system.
00:07:36 I am selecting Winx64.
00:07:41 You see it auto adds the database connection string.
00:07:47 And ye this is fine.
00:07:51 You shouldn't select produce single I think it is not working on Windows operating system.
00:07:56 I am not sure.
00:07:57 Ok.
00:07:58 Then lets publish it.
00:08:01 Ok the publish has been completed.
00:08:04 Now I will copy and paste files into my generated inetpub folder here.
00:08:16 wwwroot todoapp lets copy and paste.
00:08:21 Ok.
00:08:22 It is here.
00:08:24 Now when we open todoapp.com lets see what happens.
00:08:31 Ok.
00:08:34 As you can see it is not working.
00:08:37 And the reason of this is that when you set your database connection as like this it is
00:08:52 it will not work in a published environment.
00:08:55 This was the reason why our application why the student's application was not working.
00:09:03 And to solve this problem we need to add a user to our users in security logins.
00:09:18 Ok.
00:09:19 In here.
00:09:21 So I am going to generate as a new user as TestAppUser.
00:09:29 This will be password.
00:09:33 Lets generate a new login.
00:09:35 Let's say todoapp.
00:09:37 This is the name of the login.
00:09:41 I will set SQL authentication I am setting password.
00:09:46 I won't enforce password policy.
00:09:49 But you can also apply that.
00:09:52 And we also need to give it roles.
00:09:55 So I think I will give it serveradmin.
00:10:00 This should be sufficient.
00:10:01 I am not sure.
00:10:02 You can select all of them as well.
00:10:04 dbcreator.
00:10:05 Maybe I will also select it.
00:10:09 Or you can select all of them.
00:10:12 I will select all of them for tutorial purposes.
00:10:16 But you can select only the minimum required one for your application.
00:10:23 Now we have a new user.
00:10:26 And for this user to be for this user account to be used we need to change our settings
00:10:38 as.
00:10:41 So userid will be equal to our new user name which is todoapp.
00:10:54 And password will be equal to TestAppUser.
00:10:56 This is our password.
00:11:00 And once we make after we make this change we also need.
00:11:05 Lets run the application and test it whether it is working or not first of all.
00:11:10 Ok it is working.
00:11:14 Ok.
00:11:16 And secondly we need to republish.
00:11:18 Ok.
00:11:19 When we select all settings we need to change this string.
00:11:29 Ok.
00:11:31 Save.
00:11:33 And lets publish.
00:11:36 Ok.
00:11:40 I will just pause video while publishing.
00:11:42 Ok the republish has been completed.
00:11:44 Now I will update the files inside our todoapp folder.
00:11:50 Inside wwwroot.
00:12:06 Ok we can just keep in use files.
00:12:08 It should be good.
00:12:10 And then we refresh our.
00:12:14 Ok.
00:12:15 As you can see now our localhost is working as supposed to be.
00:12:24 Now that we have fixed our database connection problem and we are able to publish our website
00:12:32 ASP.NET MVC .NET Core application.
00:12:37 This should work in .NET Core 3 5 6 whatever the version you are using.
00:12:42 Currently this application uses let me show you properties .NET Core 6.
00:12:52 It should be same in other .NET Core as well.
00:12:56 Now I will upload all of the source code into GitHub folder.
00:13:05 Ok this is our repository that we will publish our source code.
00:13:09 So I will open GitBash.
00:13:12 And I will move to the desktop cd.
00:13:21 Ok.
00:13:23 Then we need to add the commands.
00:13:36 Ok.
00:13:38 git remote add origin.
00:13:48 Ok.
00:13:51 Oww we have added also the unnecessary files.
00:14:00 So to not add them I will make the necessary changes.
00:14:05 We don't need vs folder or object folder.
00:14:12 Ok let's close this.
00:14:16 I will just delete the git.
00:14:18 And I will need to close the application.
00:14:26 Ok.
00:14:27 And here I should delete object as well.
00:14:37 And I can delete binary as well.
00:14:41 Ok.
00:14:43 git add remote origin.
00:14:53 git remote add origin.
00:15:03 Ok.
00:15:05 Let's say initial upload.
00:15:18 Ok it is uploaded to repository.
00:15:22 As you can see the app is here.
00:15:26 It should be same as everything.
00:15:29 Yes.
00:15:30 And when we open the application from SLN it should just work fine in your computer
00:15:37 as well.
00:15:38 Make sure that you have generated a user as this or you need to change it back to localhost
00:15:52 authentication.
00:15:54 As I have shown you previously.
00:15:55 Ok.
00:15:56 Ok.
00:15:57 This is all.
00:15:58 If you also have questions please ask me through our Discord server.
00:16:04 You can find our Discord server on our channel.
00:16:10 Here you see we have official Discord link.
00:16:15 You can see also see my LinkedIn and the game I develop.
00:16:19 Please also subscribe and if you join I appreciate that.
00:16:24 Thank you so much for watching.
00:16:26 Please make comments, likes, subscribe and hopefully see you later.
Beta Was this translation helpful? Give feedback.
All reactions