Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Query String Value in ASP.NET MVC Function

I have an ASP.NET MVC app. My views use Razor. At the top of my CSHTML file, I have the following:

@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;
    }
}

I need this function to examine the query string. Specifically, I need to get the value of the "t" query string parameter. My challenge is, I cannot seem to figure out how to get access to the QueryString in this function.

How do I get the value of a query string parameter in a Razor function?

Thanks!

like image 442
JQuery Mobile Avatar asked Nov 28 '22 07:11

JQuery Mobile


2 Answers

You need to make your function non-static, since the querystring is part of the request.

You can then write

HttpContext.Request.Query["t"]
like image 29
SLaks Avatar answered Dec 06 '22 05:12

SLaks


The query string can be gotten from below.

HttpContext.Current.Request.QueryString["t"]
like image 175
scartag Avatar answered Dec 06 '22 04:12

scartag