1

I have created a simple ASP.NET Core web application with the following steps:

Created an Empty template of ASP.NET core project:

enter image description here

enter image description here

Added a wwwroot folder with an index.html page in it.

enter image description here

Startup.cs is pretty default with just one extra line added:

app.UseDefaultFiles();

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace WebApplication1
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseDefaultFiles();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });
        }
    }
}

Build the solution and pointed my IIS web site to the root of the web project:

enter image description here

All I see an empty page on running the site via IIS (no console error, permissions already give to the root of the solution):

enter image description here

I was expecting the output of index.html to have come there at the root of the application URL. Is there any special which needs to be done for an ASP.NET core project to make it running via IIS?

I would like to debug too by attaching the IIS process in Visual Studio for that given site?

Can you share what I am missing there?

The same steps seem to be working with the ASP.NET framework. I don't want to execute Run command on the VS and run the site on localhost, my goal is to run the site with IIS/ with a valid url so that I can debug too directly by attaching the process with Visual Studio solution.

Raghav
  • 8,772
  • 6
  • 82
  • 106

2 Answers2

1

To host .net site in iis first you need to publish from the visual studio and then point that publish folder as iis site folder path.

to run .net core site you need to install the .NET Core hosting bundle and ASP.NET Core Runtime.

Download and Install the Runtime and Hosting bundle as per your version.

https://dotnet.microsoft.com/download/dotnet-core/3.1

another thing is you need to use app.UseStaticFiles(); to server the index file.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseDefaultFiles();


        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseStaticFiles();

        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapGet("/", async context =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        });
    }

you could follow the below link for more detail how to host .net core app in iis:

https://learn.microsoft.com/en-us/aspnet/core/tutorials/publish-to-iis?view=aspnetcore-3.1&tabs=visual-studio

https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-3.1

Edit:

To attach iis worker process in visual studio you could follow the below steps:

1)run your site from iis.

2)go to the server node in iis double click the worker process from the middle pane.

enter image description here

you will find your site process id note that.

enter image description here

3)open visual studio as administrator

4)open your project.

5)debug-> attach to process

enter image description here

6)tick the checkbox "Show processes from all user", select w3wp.exe.you could search your process by process id.

enter image description here

7)click on attach.

Jalpa Panchal
  • 8,251
  • 1
  • 11
  • 26
0

Just a few thoughts.

  1. Running it in VS is "either" going to be using IIS Express or IIS itself, depending on your settings. So you can easily both deploy to IIS and actually debug it directly (meaning its running there) if you choose IIS.

  2. You can attach np by connecting to your IIS site using Edge/Chrome whatever, then in your VS select Debug\Attach To Process and you merely need to know for sure which process. Now if its on another server, then you need to configure remote debugging and have that installed on that machine and have your ports open to it correctly etc.

You should think about separating out debugging initially (dev time) versus needing to debug remotely (prod / test) time because it will save you allot of time.

As to your question specifically 2 things

  1. I would use app.UseFileServer(); because it puts both app.UseDefaultFiles app.UseStaticFiles in the correct order etc.

versus app.UseDefaultFiles(); alone

  1. Take out the Hello world stuff. I believe it is blocking you (but do #1 first)

lastly, you may need to install the NuGet package Microsoft.AspNetCore.StaticFiles

Cheers,

M M E G
  • 24
  • 4