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は1つしかないことに気付きました。これは、HttpContext.Currentが最初のリクエストを指していることを意味します。では、サブリクエストのクエリ文字列データを取得するにはどうすればよいですか?

ありがとう!/ビクター

4

2 に答える 2

0

クエリ文字列に対して作業する代わりに、をとして使用できstringます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 に答える
0

少なくとも今のところ、TempDataディクショナリを使用し、使用後に値を削除することで問題を解決しましたが、それでもより良い解決策に興味があります。ルートデータを渡す方法があるはずです...

/ビクター

于 2012-01-30T10:07:17.127 に答える