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

相关推荐
咖啡八杯几秒前
GoF设计模式——中介者模式
java·后端·spring·设计模式
hez20102 小时前
在 .NET 上构建超大托管数组
c#·.net·.net core·gc·clr
lizhongxuan2 小时前
多Agent之间的区别
后端
杨充4 小时前
1.面向对象设计思想
后端
IT_陈寒5 小时前
Java的Date类又坑了我一次,改用时间戳真香
前端·人工智能·后端
systemPro5 小时前
2.6亿条设备数据,历史查询从超时到50ms,我做了什么
后端
要阿尔卑斯吗5 小时前
提示词优化启示:为什么“按顺序输出“比“关键度评分“更有效
后端
她的男孩6 小时前
后台接口加密别只会 HTTPS,ForgeAdmin 的 RSA + SM4/AES 源码拆解
后端·面试·开源
极光技术熊6 小时前
Spring AI 从入门到精通:构建你的 AI 开发知识体系
后端·github
程序员cxuan6 小时前
一句话,让你用上 GPT-5.6
人工智能·后端·程序员