【问题标题】:Reading Parameters from URL从 URL 读取参数
【发布时间】:2011-05-29 23:18:52
【问题描述】:

我一直在谷歌搜索以找到一种从 URL 读取参数值的方法,但没有成功。在 ASP.NET Webform 中,我们使用 Request.Querystring["name"] 来获取值。如何在 MVC3 中做到这一点?

我需要访问 HtmlHelper 类中的参数。任何人请。

互联网上对此没有明确的答案。奇怪……

【问题讨论】:

    标签: url asp.net-mvc-3 parameters query-string


    【解决方案1】:

    我仍然在 MVC3 中使用HttpContext.Current.Request.QueryString...

    if (!Request.QueryString["ParameterName"].IsEmpty())
    {
      // Do something only if URL parameter "ParameterName" is not empty...
    }
    

    例如:

    http://192.168.1.106:7777/Measurement?sort=FatPercentage&sortdir=DESC

    if (!Request.QueryString["sort"].IsEmpty())
    {
      // sort=FatPercentage. It's not empty and this code block will be executed
    }
    

    【讨论】:

    • @Leniel Macaferi,我仍然无法获取参数值:(
    • @pirzada:有什么问题?任何错误信息?怎么回事?
    • @Leniel Macaferi ,尽管 URL 中有一个 name 参数,但我得到了 null 值。 if (HttpContext.Current.Request.QueryString["name"].IsEmpty()) { var a = HttpContext.Current.Request.QueryString["name"].; }
    • 我不认为我理解你的问题。为什么不在您的操作方法中定义两个字符串参数,称为“sort”和“sortdir”?然后您可以使用 (sort == "") 或其他方法检查是否为空。
    • @Marcelo Zabani:我只是想展示一个示例来说明如何检查查询字符串参数...
    猜你喜欢
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2018-06-27
    相关资源
    最近更新 更多