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

转载:.NET Core MVC 中 Controller 中让页面跳转的方法

  • 方式一:

    在控制器的方法内部结尾使用 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" });

posted @   户的博客  阅读(4248)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示