Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core application debugging without publishing on IIS

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.

like image 412
MapleStory Avatar asked Aug 12 '16 04:08

MapleStory


People also ask

How to debug ASP NET Core apps in IIS?

You should now be able to debug your application with IIS. Make sure to set your build configuration to Debug, and the profile to IIS. Then click the run button to start your application: There you have it. You can now officially debug your ASP.NET Core apps within IIS.

How to debug on local IIS?

To debug on local IIS, you must meet the following requirements: 1 If it's not installed, install the ASP.NET and web development workload. ... 2 Run Visual Studio as an administrator. 3 Install and correctly configure IIS with the appropriate version (s) of ASP.NET and/or ASP.NET Core. ... 4 Make sure the app runs on IIS without errors.

How to publish an app to a folder in IIS?

The app is published to a folder. The folder's contents are moved to the IIS site's folder (the Physical path to the site in IIS Manager). Right-click on the project in Solution Explorer and select Publish. In the Pick a publish target dialog, select the Folder publish option. Set the Folder or File Share path.

Is it possible to run MVC application in IIS?

With ASP.NET MVC applications on the .NET Framework you could just spin up a new Website in full IIS that points to the MVC project's folder and it would "just work". What do you need to do to get this to work with ASP.NET Core?


2 Answers

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

like image 122
Ryan Mann Avatar answered Sep 25 '22 10:09

Ryan Mann


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.

like image 36
Sнаđошƒаӽ Avatar answered Sep 24 '22 10:09

Sнаđошƒаӽ