MVC中重定向几种方法

//1.Response.Redirect
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.Mvc;  
  
namespace MvcDemo.Controllers  
{  
    [HandleError]  
    public class HomeController : Controller  
    {  
        public ActionResult Index()  
        {  
            ViewData["Message"] = "欢迎使用 ASP.NET MVC!";  
            Response.Redirect("User/News");  
            return View();  
        }  
  
        public ActionResult About()  
        {  
            return View();  
        }  
    }  
}  

//2.Return  Redirect
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.Mvc;  
  
namespace MvcDemo.Controllers  
{  
    [HandleError]  
    public class HomeController : Controller  
    {  
        public ActionResult Index()  
        {  
            ViewData["Message"] = "欢迎使用 ASP.NET MVC!";  
            return Redirect("User/News");  
        }  
  
        public ActionResult About()  
        {  
            return View();  
        }  
    }  
}  

//3.Return RedirectToAction
RedirectToAction(“ActionName”);//该方法直接写入页面,前提必须是在改控制器下问页面如前面的Index.aspx,和About.aspx  
  
RedirectToAction(“ActionName”,"ControllerName")//该方法直接写入ActionName和ControllerName,前提必须是在改控制器下问页面如前面的Index.aspx,和About.aspx  
  
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.Mvc;  
  
namespace MvcDemo.Controllers  
{  
    [HandleError]  
    public class HomeController : Controller  
    {  
        public ActionResult Index()  
        {  
            ViewData["Message"] = "欢迎使用 ASP.NET MVC!";  
            return RedirectToAction("News","User");  
        }  
  
        public ActionResult About()  
        {  
            return View();  
        }  
    }  
}  

 

转载于:https://www.cnblogs.com/X-Jonney/p/6669648.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring MVC ,可以使用重定向和转发来进行请求的跳转。下面是设定重定向和转发的几种方式: 1. 重定向: - 使用 `RedirectView` 类:可以在控制器方法返回一个 `RedirectView` 对象,设置重定向的目标 URL。 - 使用 `RedirectAttributes` 类:可以在控制器方法重定向的目标 URL 添加到 `RedirectAttributes` 对象,并使用 `redirect:` 前缀来指示重定向。 2. 转发: - 使用 `ModelAndView` 类:可以在控制器方法返回一个 `ModelAndView` 对象,设置转发的视图名称。 - 使用 `forward:` 前缀:可以在控制器方法使用 `return "forward:/path"` 的方式来指示转发到指定的路径。 下面是一个示例,展示如何在控制器方法设定重定向和转发: ```java @Controller public class MyController { @GetMapping("/redirect") public RedirectView redirectToUrl() { RedirectView redirectView = new RedirectView(); redirectView.setUrl("https://www.example.com"); return redirectView; } @GetMapping("/redirectWithAttributes") public String redirectWithAttributes(RedirectAttributes attributes) { attributes.addAttribute("param", "value"); return "redirect:/targetUrl"; } @GetMapping("/forward") public ModelAndView forwardToView() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("forward:/targetView"); return modelAndView; } } ``` 上述代码,`/redirect` 路径的请求会被重定向到 `https://www.example.com`,`/redirectWithAttributes` 路径的请求会带着参数重定向到 `/targetUrl`,`/forward` 路径的请求会被转发到 `targetView` 视图。 需要注意的是,在设定重定向和转发时,可以使用绝对路径或相对路径,具体根据需求来确定。同时,还可以在路径使用占位符和路径参数来实现动态的跳转。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值