NetCore全局调整路由拦截处理(系统下线配置)

Startup.cs

Configure节

代码如下

---

// 在MVC之前添加自定义重定向中间件

            app.Use(async (context, next) =>

            {

                try

                {

                    var routePath = context.Request.Path.ToString().ToLower();

                    // 检查是否请求的是/forme路径

                    if (routePath.StartsWith("/home/")

                            //|| routePath.StartsWith("/inc/face")

                            || routePath.StartsWith("/hr/")

                            || routePath.StartsWith("/res/")

                            )

                    {

                        // 如果是,则继续处理下一个中间件

                        await next.Invoke();

                    }

                    else

                    {

                        Console.WriteLine("gotonewerp {0} {1}", routePath, context.Request.Query);

                        // 如果不是,则重定向到指定页面,例如 /home/index

                        context.Response.Redirect("/home/gotoNewErp");

                    }

                    await next.Invoke();

                }

                catch (Exception ex)

                {

                    // Log the exception details

                    Console.WriteLine("gotonewerp error occurred: {0}", ex.Message);

                    context.Response.StatusCode = 500; // Set the status code to 500

                    //await context.Response.WriteAsync("Internal Server Error"); // Optional: Write a response message

                    await next.Invoke();

                }

            });


            app.UseMvc(routes =>