4

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!

3 Answers 3

20

The query string can be gotten from below.

HttpContext.Current.Request.QueryString["t"]
Sign up to request clarification or add additional context in comments.

2 Comments

In MVC 6, use Context.Request.Query["t"]
@jbyrd Lucky you ... it won't have a go-live license till nov/dec and won't RTM till early next year. Most orgs won't risk it.
3

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

You can then write

HttpContext.Request.Query["t"]

Comments

0

You should really be doing this in the controller and pushing it through the model. But if you insist, you can simply use:

<%= Request["t"] %>

But why not read it in your controller?!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.