可以请求从htmlhelper访问查询字符串

您好可以在HTMLHelper扩展方法中访问查询字符串。 我们需要根据请求中的查询字符串进行不同的呈现。

是的,通过当前上下文,这是HTML Helper上的属性。

public static string DoThis(this HtmlHelper helper) { string qs = helper.ViewContext.HttpContext.Request.QueryString.Get("val"); //do something on it } 

当然:

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

您可以通过HttpContext对象访问查询字符串。 像这样……

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