C# 可以从htmlhelper访问请求查询字符串吗
嗨C# 可以从htmlhelper访问请求查询字符串吗,c#,asp.net-mvc,html-helper,C#,Asp.net Mvc,Html Helper,嗨 可以在HTMLHelper扩展方法中访问查询字符串。我们需要根据请求中的查询字符串进行不同的渲染。您可以通过HttpContext对象访问查询字符串。就像这样 string itemVal = System.Web.HttpContext.Current.Request.QueryString["item"]; 是的,通过当前上下文,它是HTML帮助器上的一个属性 public static string DoThis(this HtmlHelper helper) { string
可以在HTMLHelper扩展方法中访问查询字符串。我们需要根据请求中的查询字符串进行不同的渲染。您可以通过HttpContext对象访问查询字符串。就像这样
string itemVal = System.Web.HttpContext.Current.Request.QueryString["item"];
是的,通过当前上下文,它是HTML帮助器上的一个属性
public static string DoThis(this HtmlHelper helper)
{
string qs = helper.ViewContext.HttpContext.Request.QueryString.Get("val");
//do something on it
}
当然可以:
注意:
HttpContext.Current
应尽可能避免在ASP.NET核心中使用
public static MvcHtmlString Foo(this HtmlHelper htmlHelper)
{
var value = htmlHelper.ViewContext.HttpContext.Request["paramName"];
...
}