.Net Core 3网站托管404错误

时间:2020-02-21 06:42:19

标签: c# iis .net-core

我有一个要托管的.Net core 3网站。我已经成功部署了一个站点,并尝试部署另一个站点,但没有运气。如果我将工作站点的文件夹指向新的站点文件夹,并且我也共享工作站点的应用程序池,则它可以工作,因此我100%确信它是IIS配置问题。尝试浏览时出现404错误。新站点的唯一区别是它在不同的端口上并且没有安装证书,因此也许我的启动中的SSL代码正在起作用

public class Startup
{

    private string path = string.Empty;
    private string connection = string.Empty;

    public class ErrorDetails
    {
        public int StatusCode { get; set; }
        public string Message { get; set; }
        public override string ToString()
        {
            return JsonConvert.SerializeObject(this);
        }
    }

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddHttpsRedirection(options =>
        {
            options.RedirectStatusCode = StatusCodes.Status308PermanentRedirect;
            options.HttpsPort = 443;
        });

        //services.AddResponseCaching();
        services.AddMvc(option => option.EnableEndpointRouting = false).AddJsonOptions(options => {
            options.JsonSerializerOptions.IgnoreNullValues = true;

        });

        services.AddControllersWithViews();

        services.AddHttpContextAccessor();

        services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(options => {
                    options.LoginPath = "/Account/Login/";
                    options.AccessDeniedPath = "/Account/Denied";
                    //options.Cookie.IsEssential = true;
                }); 
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthentication();
        app.UseAuthorization();

        //app.UseResponseCaching();

        app.UseMvc();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapDefaultControllerRoute();

            //endpoints.MapControllerRoute(
            //    name: "default",
            //    pattern: "{controller=Home}/{action=Index}/{id?}");
        });

        app.UseExceptionHandler(appError =>
        {
            appError.Run(async context =>
            {
                context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                context.Response.ContentType = "application/json";

                var contextFeature = context.Features.Get<IExceptionHandlerFeature>();

                var exceptionFeature = context.Features.Get<IExceptionHandlerPathFeature>();

                if (exceptionFeature != null)
                {
                    // Get which route the exception occurred at
                    string routeWhereExceptionOccurred = exceptionFeature.Path;

                    // Get the exception that occurred
                    Exception exceptionThatOccurred = exceptionFeature.Error;

                    Serilog.Log.Error($" Web API error in controller:{routeWhereExceptionOccurred}" + Environment.NewLine + exceptionThatOccurred.ToString() + Environment.NewLine + new String('-', 100));

                    await context.Response.WriteAsync(new ErrorDetails()
                    {
                        StatusCode = context.Response.StatusCode,
                        Message = $"Unhandled exception at location:{routeWhereExceptionOccurred}, please see log files located in:{path} for details" + Environment.NewLine + Environment.NewLine + "Exception:" + exceptionThatOccurred.ToString()

                    }.ToString());
                }

            });
        });
    }
}

1 个答案:

答案 0 :(得分:0)

据我所知,services.AddHttpsRedirection方法将强制将http请求重定向到https。

但是,如果您未在IIS服务器中绑定https,这意味着没有https网址,那么它将返回404错误。

要解决此问题,如果您不想为Web应用程序启用https,建议您删除此方法。

相关问题