首页 > 解决方案 > 开始调试时在 Home/Error 上自动重定向

问题描述

我有一个非常奇怪的问题。突然,当我开始调试我的项目时,我被重定向到Home/Error而不是Home/Index 没有任何明显的错误或消息。

该应用程序非常简单,因为我今天刚刚创建了它。

这是我的启动:

public Startup(IConfiguration configuration)
{
    ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault;
    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.AddSingleton<IConfiguration>(Configuration);

    services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Optimal);
    services.AddResponseCompression(options =>
    {
        options.EnableForHttps = true;
        options.Providers.Add<GzipCompressionProvider>();
    });

    // Without AddNewtonsoftJson actions that receive JSON Objects Body will return error 406
    services.AddControllersWithViews().AddNewtonsoftJson();

    services.AddSingleton<ITempDataProvider, CookieTempDataProvider>();
    services.AddSession(options =>
    {
        options.IdleTimeout = TimeSpan.FromMinutes(60);
    });
    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    services.AddHttpContextAccessor();

    services.AddTransient<IElasticsearchService, ElasticsearchService>();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public static 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();

    // Enable compression (must be before UseStaticFiles)
    app.UseResponseCompression();

    app.UseDefaultFiles();
    app.UseStaticFiles();
    app.UseRouting();
    app.UseAuthorization();

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

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

这是(目前唯一的)我的控制器:

public HomeController(ILogger<HomeController> logger, IHttpContextAccessor httpContextAccessor, IElasticsearchService elasticsearchService)
    : base (logger, httpContextAccessor, elasticsearchService) { }

public async Task<IActionResult> Index(Dictionary<string, string> @params)
{
    // some code

    return View();
}

public IActionResult Privacy()
{
    return View();
}

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
    string errorMessage = string.Empty;
    if (TempData["ErrorMessage"] != null)
    {
        errorMessage = TempData["ErrorMessage"].ToString();
        TempData["ErrorMessage"] = null;
    }
    ViewBag.ErrorMessage = errorMessage;

    return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}

标签: c#asp.net-core

解决方案


我想我自己解决了这个问题,也许问题是,我使用了 a RedirectToPermanent()Index从那时起,它总是将我重定向到Error.

我清理了cachetemporary files现在它似乎工作。


推荐阅读