查询字符串和渲染操作
当发送特定的查询字符串参数时,我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
字符串
作为您的模型
,而不是针对查询字符串进行操作。Instead of working against the query string you could use a
string
as yourModel
.至少现在我通过使用 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