【问题标题】:How to parse query string MVC3如何解析查询字符串 MVC3
【发布时间】:2013-10-29 10:29:44
【问题描述】:

我有一个这样的网址:

http://localhost:9562/Account/LogOn?ReturnUrl=%2fCabinet%2fCabinet

我需要将其解析为:

Cabinet/Cabinet

我看过了 thisthis 但我不明白如何在我的示例中使用它。

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-3 query-string


    【解决方案1】:

    最简单的方法是将其作为LogOn 操作中的参数接受:

    public class AccountController : Controller
    {
        public ActionResult LogOn(string ReturnUrl = "")
        {
        }
    }
    

    注意,提供默认值(即= "")允许操作执行,即使请求中不存在查询参数。

    或者,您可以通过控制器的 Request 属性访问它:

    public class AccountController : Controller
    {
        public ActionResult LogOn()
        {
            string request = this.Request.QueryString["ReturnUrl"];
        }
    }
    

    【讨论】:

      【解决方案2】:

      试试这个:

       string r = Request.QueryString["ReturnUrl"].Substring(1);
       Response.Write(r);
      

      【讨论】:

      • 我试过了,还是不行。但我用 Uri 试过,而不是用字符串 (Uri myUri = new Uri(returnUrl)) 并且它有效。谢谢
      猜你喜欢
      • 1970-01-01
      • 2019-05-03
      • 2011-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-07
      相关资源
      最近更新 更多