查询字符串和渲染操作

发布于 2024-12-25 13:22:10 字数 654 浏览 7 评论 0原文

当发送特定的查询字符串参数时,我在 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 指向第一个请求。那么 - 如何获取子请求的查询字符串数据?

谢谢! /胜利者

I'm setting a class on my html tag when a specific query string argument is sent, right now I'm doing it like this (Razor view master page):

@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>
}

Works fine for normal requests, but not when I call the page using RenderAction, like

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

After some looking around I have realized that there only is one actual HttpContext, which means that HttpContext.Current points to the first request. So - how do I get the query string data for the sub request?

Thanks!
/Victor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

演多会厌 2025-01-01 13:22:10

您可以使用字符串作为您的模型,而不是针对查询字符串进行操作。

@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);
}

Instead of working against the query string you could use a string as your 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);
}
在梵高的星空下 2025-01-01 13:22:10

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

/Victor

At least for now I solved the problem by using the TempData dictionary and removing the value after use, but I am still interested in a better solution. Seems like there should be a way to get routedata passed...

/Victor

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文