簡體   English   中英

當 ASP.NET Core MVC 中出現 404 錯誤時,重定向不起作用

[英]Redirection is not working when there is 404 error in ASP.NET Core MVC

當出現 404 錯誤時,我想將用戶重定向到操作方法頁面。 例如,我的域是www.test.com ,但是當用戶導航到www.test.com/abc/def時,我想將用戶重定向到特定的 404 頁面。

這是我在program.cs中的代碼:

app.Use(async (context, next) =>
{
    EnteredPath = context.Request.HttpContext.Request.Path.ToString().Remove(0, 1);
    await next();

    if (context.Response.StatusCode == 404)
    {
        context.Request.Path="/URL/checkRawUrl";
        await next();
    }
});

它不起作用 - 我的 controller 名稱是URL ,操作名稱是checkRawUrl

感謝和問候

注意它必須在路由配置之前添加,如 app.UseRouting();.... 請檢查它。

app.Use(async (context, next) =>
{
    string EnteredPath = context.Request.HttpContext.Request.Path.ToString().Remove(0, 1);
    await next();

    if (context.Response.StatusCode == 404)
    {
        context.Request.Path = "/URL/checkRawUrl";
        await next();
    }
});
app.UseRouting();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM