.net core mvc 控制器中页面跳转

  • 方式一:

    在控制器的方法内部结尾使用 return View(); 来打开与方法同名的页面,如:

    public ActionResult Login()

    {

    return View();

    }

    该写法打开 Login 页面。

  • 方式二:

    可以添加参数来显式地指定要跳转的页面,如:

    return View("Register");

    该写法跳转到系统控制器下的 Register 页面。

  • 方式三:

    使用 RedirectToAction 方法,跳转到指定的控制器的指定页面,如:

    public async Task<IActionResult> Logout()

    {

    await HttpContext.SignOutAsync("Cookies");

    return RedirectToAction("Index", "Home");

    }

    该写法跳转到 Home 控制器的 Index 页面。

  • 方式四:

    使用 Redirect 方法,如:

    return Redirect("/Home/Index"); //临时重定向

    return RedirectPermanent("/Home/Index"); //永久重定向

    效果和方式三一样。

  • 方式五:

    使用 RedirectToRoute 方法:

    return RedirectToRoute(new { Controller = "Home", Action = "Index", ID = "1" });

相关推荐
ayqy贾杰15 分钟前
Claude Fable 5 提示词泄漏,抓紧学习下
前端·后端·面试
智码看视界21 分钟前
Day18 SpringBoot自动配置原理:从@SpringBootApplication开始
java·spring boot·后端·自动装配
程序员爱钓鱼35 分钟前
Rust 常量与静态变量详解:const 与 static 的区别
后端
戮漠summer12 小时前
Missing Semester 计算机教育中缺失的一课 Lecture 01 Shell
开发语言·后端·scala
LibraJM13 小时前
一种适合程序员的 Agent 协作方式的实践
c#·copilot·agents
IT_陈寒13 小时前
Redis的KEYS命令把我搞崩溃了,改用SCAN才活过来
前端·人工智能·后端
长不胖的路人甲14 小时前
SpringCloud 服务雪崩、熔断、降级
后端·spring·spring cloud
爱折腾的小黑牛15 小时前
小程序多人协作的权限设计:三级权限的实现
后端
蓝银草同学15 小时前
Stream 实战:博客列表排序、过滤与分页(AI 辅助学习 Java 8)
java·前端·后端