查询字符串变量在MVC控制器中的使用
本文关键字:控制器 MVC 字符串 变量 查询 | 更新日期: 2023-09-27 18:03:34
我想在MVC控制器中接受我的querystring值。我也试着把它作为一个参数,但它也不接受。因为我想确认这个登录请求来自收件箱。
http://localhost:58692/Account/Login?returnFrom=#inbox
My MVC controller
[HttpPost]
[AllowAnonymous]
public ActionResult Login(User user, string returnUrl)
{
if (Request.QueryString["returnFrom"] != null) // this comes as a null
{
}
}
它不起作用,因为#
之后的一切都是客户端。URL中的#
定义了到命名锚的链接,#
后面的查询字符串不会发送到IIS服务器。由于这是客户端,您需要从URL中转义#
—您无法在服务器上获得它,因为浏览器已经剥离了它。试试这个:
http://localhost:58692/Account/Login?returnFrom=inbox
要做到这一点,你需要编写一些JavaScript代码,所以你可以使用这种技术
您需要有两个登录操作(GET和POST):
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
if (Request.QueryString["returnFrom"] != null)
{
// return the Login view...
}
}
[HttpPost]
public ActionResult Login(User user)
{
// Check if the user is authorized...
}
并且,正如Sirwan所说,您需要从URL中删除#
:
http://localhost:58692/Account/Login?returnFrom=inbox