1

当发送特定的查询字符串参数时,我正在我的 html 标记上设置一个类,现在我正在这样做(Razor 视图母版页):

@if (HttpContext.Current.Request.QueryString.AllKeys.Contains("Foo") && HttpContext.Current.Request.QueryString["Foo"] == "Bar") {
  //Do something when Foo=Bar (like http://server/route?Foo==Bar)
  <html class="bar-class">
}
else {
  //Normal html tag
  <html>
}

适用于正常请求,但当我使用 RenderAction 调用页面时,例如

//Other view, the one requested by the user
@Html.RenderAction("Index", "Route", new {Foo="Bar"})

环顾四周后,我意识到只有一个实际的 HttpContext,这意味着 HttpContext.Current 指向第一个请求。那么 - 如何获取子请求的查询字符串数据?

谢谢!/胜利者

4

2 回答 2

0

至少现在我通过使用 TempData 字典并在使用后删除值解决了这个问题,但我仍然对更好的解决方案感兴趣。似乎应该有一种方法可以让路由数据通过......

/胜利者

于 2012-01-30T10:07:17.127 回答
0

您可以使用 astring作为您的Model.

@model string
@if (!string.IsNullOrWhiteSpace(Model) && Model == "Bar") {
  //Do something when Foo=Bar (like http://server/route?Foo==Bar)
  <html class="bar-class">
}
else {
  //Normal html tag
  <html>
}

public ActionResult Route(string foo){
  return View(foo);
}
于 2012-01-05T12:44:04.743 回答