.net的controller中如何让页面跳转

2025-03-26 12:59:43

介绍了 .NET Core MVC 中 Controller 中让页面跳转的方法。(独立观察员)

工具/原料

VisualStudio

安装VS时安了 .NET Core

方式

1、方式一:在控制器的方法内部结尾使用return View(); 来打开与方法同名的页面,如:public ActionResult Login(){ return View();}该写法打开 Login 页面。

2、方式二:可以添加参数来显式地指定要跳转的页面,如:return View("Register");该写法跳转到系统控制器下的Register 页面。

3、方式三:使用RedirectToAction 方法,骇螺搭翳跳转到指定的控制器的指定页面,如:public as烤恤鹇灭ync Task<IActionResult> Logout(){ await HttpContext.SignOutAsync("Cookies"); return RedirectToAction("Index", "Home");}该写法跳转到 Home 控制器的 Index 页面。

4、方式四:使用Redirect 方法,如:return Redirect("/Home/Index"); //临时重定向return Redir髫潋啜缅ectPermanent("/Home/Index");//永久重定向效果和方式三一样。

5、方式五:使用RedirectToRoute 方法:return RedirectToRoute(new { Controller = "Home", Action = "Index", ID = "1" });

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢