10

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.

Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
MapleStory
  • 628
  • 3
  • 11
  • 22
  • 1
    You can't point IIS at the solution code because the solution code doesn't contain all the files for DotNet Core to run, not even in the bin folder. When you run DotNet Publish it pulls in everything else it needs so the published site is what you have to point IIS at. See my answer for an alternative workflow to this concept using dotnet run. – Ryan Mann Aug 12 '16 at 14:30
  • Take a look at my answer. – Sнаđошƒаӽ Oct 29 '19 at 02:48

5 Answers5

5

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.

enter image description here

Ryan Mann
  • 5,178
  • 32
  • 42
  • Also worth noting, that if you do run the site in IIS, you can't edit the views in real time and refresh the page. Because DotNet Core publishes to a publish directory and that's the directory you have to point IIS at for the site to run. Which means you would need to edit the published views to see real time changes. Dotnet run is the new way to do this stuff. With dotnet run you can edit your views and see changes with page refreshes, etc. – Ryan Mann Aug 12 '16 at 14:28
  • 1
    What if we need to run multiple projects simultaneously, like a web api and an mvc project that consumes that web api, how to do this using dotnet run? – Zeshan Munir Jan 27 '17 at 04:58
  • I'll check on this when I get home, but for now just Create a batch file for each project that changes to the project directory and sets the ASPNETCORE_URLS environment url to a different url for each and then run "dotnet run" in the batch file. Just run both of those batch files one after the other. And you'll have two console windows running to separate instances of kestrel. If you need to debug one, attach to the dotnet run process for the one you want to debug. To know that you could have the batch file output the PID after running dotnet run. – Ryan Mann Jan 27 '17 at 18:44
  • Ryan, did you get time to check it? – Zeshan Munir Feb 02 '17 at 07:10
  • No, I forgot about it. – Ryan Mann Feb 02 '17 at 14:34
  • Another thing you can do though, is build your web api inside a standalone class library (all the controllers in the class library etc). Also the config etc in the class library. Then just reference that in your MVC project and run it in the MVC web app. Then design the MVC app to only load the class library for the web api in debug mode and use Json Setting transformations for your publishing profiles to have different environment settings etc. You still have a web api project, but would only use it in stage/qa/production. I think I would do this even if I have 20 api's. Easier. – Ryan Mann Feb 02 '17 at 14:37
  • My recommendation in .Net core for multiple projects, is to design them all to run in one project during development. Design all your API's as class libraries, and configure them all to run in development mode in your main application, so when you "dotnet run" your project in Development all your apis are in that process on sub urls like "/api/people" and "/api/auth" etc. For production, have separate host projects for each API where you can run them where you need to. Development environments should be simple imo, run everything in one process. Use async, helps a lot. – Ryan Mann Jul 03 '18 at 20:04
5

Follow these steps to be able to achieve what you want.

  1. In launchSettings.json, add a property named iis under iisSettings, like so:

    "iis": {
      "applicationUrl": "http://my.aspnetcoreapp.com"
    }
    
  2. 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"
      }
    }
    
  3. 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".

  4. 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.
  5. Add a loop back entry in the hosts file (for Windows C:\Windows\System32\drivers\etc\hosts)

    127.0.0.1 my.aspnetcoreapp.com

  6. 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...".

  7. 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.

Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
1

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.

GJBisschop
  • 315
  • 1
  • 3
  • 12
Set
  • 47,577
  • 22
  • 132
  • 150
0

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:

  1. 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
  2. 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 }
  3. 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)

  4. 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

  5. Build the application and run the URL - http://localhost:5000

  6. 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

Maulik Boghara
  • 129
  • 1
  • 5
-3

Just run Ctrl + F5 and you can make code changes while running the site without restarting it.

Rikard Askelöf
  • 2,762
  • 4
  • 20
  • 24