can request querystring be accessed from htmlhelper

天涯浪子 提交于 2019-12-08 14:29:53

问题


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


回答1:


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
}



回答2:


Sure:

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



回答3:


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

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


来源:https://stackoverflow.com/questions/4145111/can-request-querystring-be-accessed-from-htmlhelper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!