在ASP.NET MVC函数中获取查询字符串值

JQu*_*ile 3 razor asp.net-mvc-4

我有一个ASP.NET MVC应用程序.我的观点使用Razor.在我的CSHTML文件的顶部,我有以下内容:

@functions
{
    public static HtmlString IsSelectedCss(string name)
    {
        string selected = ""; // Need to get value of "t" from query string

        HtmlString attribute = new HtmlString("");
        if (selectedTab.Equals(name, StringComparison.InvariantCultureIgnoreCase))
        {
          attribute = new HtmlString("class=\"active\"");
        }                
        return attribute;
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要这个函数来检查查询字符串.具体来说,我需要获取"t"查询字符串参数的值.我的挑战是,我似乎无法弄清楚如何在此函数中访问QueryString.

如何在Razor函数中获取查询字符串参数的值?

谢谢!

sca*_*tag 18

查询字符串可以从下面获取.

HttpContext.Current.Request.QueryString["t"]
Run Code Online (Sandbox Code Playgroud)

  • 在 MVC 6 中,使用 `Context.Request.Query["t"]` (2认同)