23

Hi Can query string be accessed in HTMLHelper extension methods. We need to render differently depending on the querystring in the request.

3 Answers 3

36

Yes, through the current context, which is a property on HTML Helper.

public static string DoThis(this HtmlHelper helper)
{
   string qs = helper.ViewContext.HttpContext.Request.QueryString.Get("val");
   //do something on it
}
Sign up to request clarification or add additional context in comments.

Comments

8

Sure:

public static MvcHtmlString Foo(this HtmlHelper htmlHelper)
{
    var value = htmlHelper.ViewContext.HttpContext.Request["paramName"];
    ...
}

Comments

5

You can access the querystring via the HttpContext object. Like so...

string itemVal = System.Web.HttpContext.Current.Request.QueryString["item"];

1 Comment

NOTE: HttpContext.Current should be avoided in ASP.NET Core where possible

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.