asp.net-mvc - 使用控制器上下文之外的查询参数重定向到控制器
问题描述
我在网上看了很多次,但一直没能找到解决我目前困境的办法。正如标题所示,我正在尝试使用以下代码将用户重定向到我的视图控制器之一:
// this is in a service that's beyond the Controller scope
httpContext.Response.Redirect("/Login");
这很好用;但是,我还需要传入一个查询参数。本质上,我希望做这样的事情:
// this is in a service that's beyond the Controller scope
httpContext.Response.Redirect("/Login?NoAccess=true");
我的视图控制器如下所示:
[AllowAnonymous]
[HttpGet("~/Login")]
public async Task<IActionResult> Index([FromQuery]bool noAccess = false)
{
// implementation
}
正如人们可能想象的那样,这不起作用,并且该值没有传递给我的控制器。
是否可以使用控制器上下文之外的重定向来传递查询参数?
谢谢,鲁本
解决方案
尝试使用RedirectToAction
orRedirectToRoute
代替。将返回一个 IActionResult 来进行重定向。使用它,您可以指定带有参数的对象。像这样:
public IActionResult Action()
{
return this.RedirectToAction("Index", "ControllerName", new {
noAccess = true
});
}
推荐阅读
- python - PyQt6 的“QMessageBox.Yes”的替代方案
- python - 在Python的一个范围内以iso 8601格式生成n个日期的随机列表
- java - 如何在“movie_added_by”表中添加movie_id和user_id
- node.js - Mongoose ObjectId 在打字稿中不匹配
- laravel - 如何将内联模板与 VueJS 集成?
- c# - 在 C# 类库中,在其自己的线程中关闭 WPF LogWatcher 窗口,停止主线程
- php - 如何在 WooCommerce 我的帐户的编辑页面上添加自定义昵称字段
- node.js - 将 json 转换为 excel 并使用节点 js 将其上传到 s3
- java - JDK 11 的 Java 项目字节码版本
- java - How to confgure the PGProperty TCP_KEEP_ALIVE that is available at HikariConfig