.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" });

相关推荐
打工仔折腾 AI2 小时前
从零写一个CAD 05:以鼠标为中心的滚轮缩放,招法能复用但顺序不能反
开发语言·后端·python·性能优化·计算机外设·swift·ai agent 实战
geovindu4 小时前
rust: Facade Pattern
开发语言·后端·设计模式·rust·外观模式
颜进强5 小时前
20 · NestJs循环依赖与 forwardRef:容器为什么在环面前会死,"占位再补齐"怎么救
前端·后端·ai编程
颜进强5 小时前
19 · NestJs @Global 落地账:装饰器与 `isGlobal` 参数,各在什么场景上岗
前端·后端·ai编程
wflynn5 小时前
GitHub 今日推荐|SCSKiller:游戏着色器预编译消除卡顿
开源·c#·github·nvidia·amd·directx12
她的男孩5 小时前
分片上传的大文件人人可下载:文件模块 isPrivate 在合并时被抹成 false,另有 4 个静默坑
java·后端·架构
颜进强5 小时前
18 · NestJS动态模块与 forRoot:`imports: [ConfigModule.forRoot({...})]` 到底在 import 什么
前端·后端·ai编程
xn71335 小时前
Personal AI Agent 架构实战:Memory、权限、跨 App 与本地/云端设计
人工智能·后端·agent
谢亮_vipxieliang6 小时前
Go defer、panic与recover核心知识点
开发语言·后端·golang
颜进强6 小时前
17 · 自定义 Provider 四形态:useValue / useClass / useFactory / useExisting 到底在选什么
前端·后端·ai编程