首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >托管404错误的.Net核心3站点

托管404错误的.Net核心3站点
EN

Stack Overflow用户
提问于 2020-02-21 14:42:20
回答 1查看 125关注 0票数 0

我有一个.Net核心3网站,我正在尝试托管。我已经成功地部署了一个站点,并试图部署第二个站点,但没有运气。如果我将工作站点的文件夹指向新的站点文件夹,并且还共享了工作站点的应用程序池,它就会工作,所以我百分之百确定这是IIS配置问题。当我尝试浏览时,我得到了404个错误。新站点的唯一不同之处在于它在不同的端口上,并且没有安装证书,因此可能我启动时的SSL代码会产生影响

代码语言:javascript
复制
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());
                }

            });
        });
    }
}
EN

回答 1

Stack Overflow用户

发布于 2020-02-24 16:55:12

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

但是如果你没有在IIS服务器上绑定https,那就意味着没有https url,所以它将返回404错误。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60333364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档