I used to use asp.net mvc4 and in IIS my website's physical path would point my solution directory, and every time I update my code, I just re-build my solution and then I can use "Attach to process" (w3wp) to start debugging.
In asp.net core, when I publish my website to file system, I can run my website using IIS with no-managed code. But when I point my IIS Website to my solution code of website, it shows 502 error.
You don't need to run .Net Core in IIS to get easy debugging etc like we used to do as you described.
With .Net Core you can just open a command line at your project root and type "dotnet run"
DotNet Run uses environment variables to drive what it does. So if you want your site to run on a specific URL or port you Type:
SET ASPNETCORE_URLS=http://example.com
Or if you just want it to run on a different port
SET ASPNETCORE_URLS=http://localhost:8080
Then to set the Environment
SET ASPNETCORE_ENVIRONMENT=Development
Once all your environment variables are set, you type
dotnet run
Now to debug it, you attach to cmd.exe with dotnet run in it's Title. You'll be able to debug your code that way.
Now, if you are using Visual Studio There is a file called "launchSettings.JSON" under Properties in your project. You can configure profiles here and I have my default profiles set to Kestrel Development and then Kestrel Production, with IIS dead last so that I don't F5 run in IIS Express.
My LaunchSettings.json looks like this:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:56545/",
"sslPort": 0
}
},
"profiles": {
"Kestrel Development": {
"executablePath": "dotnet run",
"commandName": "Project",
"commandLineArgs": "dotnet run",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_URLS": "http://localhost:8080"
}
},
"Kestrel Production": {
"commandLineArgs": "dotnet run",
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_URLS": "http://localhost:8080",
"ASPNETCORE_ENVIRONMENT": "Production"
},
"executablePath": "dotnet"
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
The first Profile is what F5 uses when you press it. So when I press F5 Visual Studio launches dotnet run for me and set's the Environment and URLS as specified by the environmentVariables section of my profile in launchSettings.JSON.
Now because I have multiple Profiles I get a drop down next to the run button so I can select Kestrel Production if I want to run in Production mode locally.
Follow these steps to be able to achieve what you want.
In launchSettings.json, add a property named iis under iisSettings, like so:
"iis": {
"applicationUrl": "http://my.aspnetcoreapp.com"
}
Under the profiles section, add a new profile having commandName set to IIS. I am calling mine Local IIS. This will add a new option to the Run drop down named Local IIS.
"Local IIS": {
"commandName": "IIS",
"launchBrowser": true,
"launchUrl": "http://my.aspnetcoreapp.com",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
Create a website in IIS. Set host name to my.aspnetcoreapp.com. Also create/use an app pool for this website that has .NET CLR version set to "No Managed Code".
Set physical path of that website to the location of your asp.net core project, not the solution, unless of course if you have the project in the same folder as the solution.
Add a loop back entry in the hosts file (for Windows C:\Windows\System32\drivers\etc\hosts)
127.0.0.1 my.aspnetcoreapp.com
Go back to Visual Studio, and run the application. Make sure you have "Local IIS" profile selected from Run drop-down. This will launch the application in the browser, at the URL, after a brief loading message that says "Provisioning IIS...".
Done! From now on, you can launch your application at that URL, and can also debug by attaching to process w3wp.
You could also host your app under "Default Web Site", by specifying the ULR like localhost/MyAspNetCoreApp instead of my.aspnetcoreapp.com. If you do that, a new app pool will be created with the name MyAspNetCoreApp AppPool.
My medium article about this.
Simple answer: when you do publish, you call a script that launches the publish-iis tool (see script section in project.json).
In your project you have a web.config file with something like this:
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%"
stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/
As you see, there are placeholders "%LAUNCHER_PATH%" and %LAUNCHER_ARGS% parameters. Keep these in mind.
Now open your project.json file and you will see a "scripts" section looking something like this:
"scripts":
{
"postpublish":"dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%"
}
It tells dotnet to run the publish-iis tool after the application is published. How it works:
publish-iis tool goes to the folder where the application was published (not your project folder) and checks if it contains a web.config file. If it doesn’t, it will create one. If it does, it will check what kind of application you have (i.e. whether it is targeting full CLR or Core CLR and – for Core CLR – whether it is a portable or standalone application) and will set the values of the processPath and arguments attributes removing %LAUNCHER_PATH% and %LAUNCHER_ARGS% placeholders on the way.
Why need this?
This process will help while continuous development & debugging of the code, Here We do not need to deploy the application again & again and don't require to press F5 to run the application, Just change the code & build you can see the application working with the recent changes.
Description:
Prerequisite: Install the ASP.NET Core Runtime(Hosting Bundle) from the official Microsoft website.
Search for the .Net core Hosting bundle and choose Microsoft's official site.
Download the suitable version from the list
Create the .Net core Web applications and made the following changes.
Go to the folder > Properties > launchsettings.json > open the file and edit as below.
Add the below section under the existing "profiles" JSON.
"IIS": {
"commandName": "IIS",
"launchBrowser": true,
"launchUrl": "http://localhost:5000", //i.e. whatever the port set for iis
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
Now, change the "iisSettings" as below.
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iis": {
"applicationUrl": "http://localhost:5000",
"sslPort": 0
}
Go to the Solution explorer > Select the Project > "Alt + Enter" or "Right Click and Select Properties"
- Go to the Debug > General > Open debug launch profiles UI.
- Select the IIS profile from the list
- Make sure the AppURL, URL is correctly set as the IIS profile launch URL (i.e. http://localhost:5000)
Open the IIS > Sites > Add WebSite
- Site Name: Any Name of site
- Application Pool > Create or select a App pool with .NET CLR Version = No Managed Code
- Physical Path > Project folder path location
- Port: any port number i.e 5000
Build the application and run the URL - http://localhost:5000
Now, to Attach & debug the application, Go to the Visual Studio and Press Ctrl+Alt+P
Find the Process > W3wp.exe i.e In the search section enter 'W3'
Mark the checkbox checked > Show processes for all users
And Press the attach button.
Set the debug point.
Note: Visit the official Microsoft site for more detail. (Debug ASP.NET Core apps section)
https://learn.microsoft.com/en-us/visualstudio/debugger/how-to-enable-debugging-for-aspnet-applications?view=vs-2022
Just run Ctrl + F5 and you can make code changes while running the site without restarting it.
Related
It looks like I am being forced to chose between the two (either "watch file changes mode" or "attach to Visual Studio mode").
These are the two different "profiles" in launchSettings.json, and I have to chose one or the other:
"profiles": {
...
"Watch file changes": {
"executablePath": "dotnet.exe",
"workingDirectory": "$(ProjectDir)",
"commandLineArgs": "watch run debug",
"launchBrowser": true,
"applicationUrl": "http://localhost:5000/",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Attach to VS": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
It is less than ideal that I have to pick one, I would like to be able to apply a breakpoint ("Attach to VS" mode) and also watch file changes.
BTW, for those interested: the profile "Watch file changes" must be coordinated with the following addition to your .csproj file:
<ItemGroup>
<!-- Files that the "dotnet watch" will monitor for hot reloading: -->
<Watch Include="**\*.razor" />
<Watch Include="**\*.scss" />
<Watch Include="**\*.cshtml"/>
<Watch Include="**\*.cs" />
</ItemGroup>
I am able to debug (breakpoint, step by step, inspect variable) my Blazor Webassembly (Blazor WASM) in Visual Studio 2019. And whenever I save certain files (*.razor, *.razor.cs, *.css), dotnet will automatically rebuild the project then Chrome will automatically refresh the page. Here's what I did:
In Visual Studio, start debugging (F5) using the "IIS Express" profile. This will open a new Chrome window.
Open a command prompt in your project's directory. run dotnet watch run. This will open a new Chrome tab.
Open the website found in step 2 using the Chrome window in step 1. In other words, copy the URL found in step 2, then paste it to the Chrome address bar found in step 1.
I have created an empty web application in visual studio code. But when running it using "dotnet run" it is not opening any browser
it is only showing the below lines.
>C:\Users\viru babu\Documents\MVCcoreLinkdin>dotnet run
>Hosting environment: Production
>Content root path: C:\Users\viru babu\Documents\MVCcoreLinkdin
>Now listening on: http://localhost:5000
>Application started. Press Ctrl+C to shut down.
To open the browser automatically on http://localhost:5000 you can add a configuration to your launch.json. Add a configuration like this:
{
"name": "Chrome",
"type": "chrome",
"runtimeExecutable": "./path/to/chrome.exe",
"request": "launch",
"url": "http://localhost:5000",
"webRoot": "${workspaceRoot}", // maybe you need to adjust this entry to "C:\Users\viru babu\Documents\MVCcoreLinkdin"
"preLaunchTask": "shell: dotnet run" // add prelaunch task start (defined in tasks.json)
}
to also launch your application automatically you use the preLaunchTask. To do that you need to configure a custom task in the tasks.json that runs your command dotnet run.
I want to start my app from the command line and automatically start a browser and show a default page (as it is run from IDE). I tried with this command:
dotnet run --launch-profile "MyApp"
In My app launchSettings.json I have defined:
"MyApp": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:53348/"
}
But the browser does not start.
Basically, in this case it is possible to run two programs in a batch file one after another, and this would solve a case.
In PowerShell, you can combine the two commands into a single line:
dotnet run --launch-profile MyApp | start chrome http://localhost:5000
How can I setup a .Net Core 1.0 project to use Local IIS instead of IIS Express when debugging?
I have tried modifying launchSettings.json file in various ways. For example, replacing all occurrences of IIS Express with Local IIS and updating the applicationUrl and launchUrl to use my custom localhost http://sample.local (I have updated the host file and configured IIS manager already) but not happy.
Default settings of Properties/launchSettings.json file:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:38601/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"SampleApp": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
You currently cannot directly use IIS to host an ASP.NET Core application while developing, as the development folder does not provide all of the necessary files IIS needs to host. This makes running an ASP.NET Core in a development environment a bit of a pain.
As pointed out in this article by Rick Strahl, there aren't many reasons to try and do this. IIS does very little when running ASP.NET Core apps - in fact your application no longer runs directly in the IIS process, instead it runs in a completely separate console application hosting the Kestrel web server. Therefore you really are running in essentially the same environment when you self host your console application.
If you do need to publish your app, you can do so to a local folder, using either the dotnet command line, or using the Visual Studio tools.
For example, if you want to publish to the C:\output folder, you can use the following command:
dotnet publish
--framework netcoreapp1.0
--output "c:\temp\AlbumViewerWeb"
--configuration Release
You can then point your IIS Site at the output folder. Ensure that you set the application pool CLR version to No Managed Code and that the AspNetCoreModule is available.
For more details, see https://docs.asp.net/en/latest/publishing/iis.html
I have updated my ASP.NET 5 project to beta 8, and we are now supposed to the following web command
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel"
},
Now i have updated my project with the environment variables.
This has also updated my launchSettings.json file, as so
{
"profiles": {
"web": {
"commandName": "web",
"environmentVariables": {
"ASPNET_ENV": "Development"
}
}
}
}
But for some reason, every time I run the command dnx web it says that the hosting environment is Production. Why is it not starting in development mode?
The settings in launchSettings.json are only used by VS. If you run from a console, you have to set that environment variable manually.
CMD:
set ASPNET_ENV=Development
dnx web
PS:
$env:ASPNET_ENV=Development
dnx web
Adding to #Victor Hurdugaci answer, you could also avoid "messing" with current environment by passing needed variables on command line.
Inside project.json file, say that you have a web-dev command specific for development environment:
"commands": {
"web-dev": "Microsoft.AspNet.Server.Kestrel
--ASPNET_ENV Development --Hosting:Environment Development
--config hosting.Development.json",
},
where you can see how both ASPNET_ENV, Hosting:Environment are set, as well as invoking a specific hosting.json configuration.
NOTE: command is split on several lines just for readability, join again before actually pasting in JSON file.
The command: set ASPNET_ENV=Development is now obsolete instead you can use CMD:
set ASPNETCORE_ENVIRONMENT=Development