Running a simple ASP.NET Core app with IIS giving a blank page

99封情书 提交于 2021-01-28 21:09:51

问题


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

Created an Empty template of ASP.NET core project:

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

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:

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

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.


回答1:


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,




回答2:


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://docs.microsoft.com/en-us/aspnet/core/tutorials/publish-to-iis?view=aspnetcore-3.1&tabs=visual-studio

https://docs.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.

you will find your site process id note that.

3)open visual studio as administrator

4)open your project.

5)debug-> attach to process

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

7)click on attach.



来源:https://stackoverflow.com/questions/61753264/running-a-simple-asp-net-core-app-with-iis-giving-a-blank-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!