使用IIS运行一个简单的ASP.NET Core应用并显示空白页面 [英] Running a simple ASP.NET Core app with IIS giving a blank page

查看:180
本文介绍了使用IIS运行一个简单的ASP.NET Core应用并显示空白页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下步骤创建了一个简单的ASP.NET Core Web应用程序:

创建了一个ASP.NET核心项目的空模板:

添加了其中带有index.html页的wwwroot文件夹.

Startup.cs是非常默认的,只添加了一行:

app.UseDefaultFiles();

 使用Microsoft.AspNetCore.Builder;使用Microsoft.AspNetCore.Hosting;使用Microsoft.AspNetCore.Http;使用Microsoft.Extensions.DependencyInjection;使用Microsoft.Extensions.Hosting;命名空间WebApplication1{公共类创业{//此方法由运行时调用.使用此方法将服务添加到容器.//有关如何配置应用程序的更多信息,请访问https://go.microsoft.com/fwlink/?LinkID=398940公共无效ConfigureServices(IServiceCollection服务){}//此方法由运行时调用.使用此方法来配置HTTP请求管道.公共无效配置(IApplicationBuilder应用程序,IWebHostEnvironment env){app.UseDefaultFiles();如果(env.IsDevelopment()){app.UseDeveloperExceptionPage();}app.UseRouting();app.UseEndpoints(endpoints =>{endpoints.MapGet("/",异步上下文=>{等待context.Response.WriteAsync("Hello World!");});});}}} 

构建解决方案,并将我的IIS网站指向Web项目的根:

我在通过IIS运行网站时看到一个空白页面(无控制台错误,权限已授予解决方案的根目录):

我期望index.html的输出位于应用程序URL的根目录.为了使ASP.NET核心项目通过IIS运行,是否需要做任何特殊的事情?

我也想通过在给定站点的Visual Studio中附加IIS进程来进行调试吗?

你能分享我在那儿想念的吗?

相同的步骤似乎适用于ASP.NET框架.我不想在VS上执行Run命令并在localhost上运行站点,我的目标是使用带有有效URL的IIS/运行站点,以便我也可以通过将进程附加到Visual Studio解决方案来直接进行调试.

解决方案

要在iis中托管.net网站,首先需要从Visual Studio中发布,然后将发布文件夹指向iis站点文件夹路径.

要运行.net Core网站,您需要安装.NET Core托管捆绑软件和ASP.NET Core Runtime.

根据您的版本下载并安装Runtime and Hosting软件包.

您会发现您的站点进程ID请注意.

3)以管理员身份打开Visual Studio

4)打开您的项目.

5)debug->附加到进程

6)选中显示所有用户的进程"复选框,选择w3wp.exe.您可以按进程ID搜索进程.

7)单击附加.

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.

解决方案

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.

这篇关于使用IIS运行一个简单的ASP.NET Core应用并显示空白页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆