1

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

Victor
  • 3,999
  • 3
  • 24
  • 27

2 Answers2

0

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);
}
Mark Coleman
  • 40,542
  • 9
  • 81
  • 101
  • The problem is I need this generic - I will use in the layout for _every_ request, and the model needs to be dictated by the actual view. So I really need to access the raw query string (or similair)... Maybe a filter could be an idea, but I dont know how... – Victor Jan 09 '12 at 10:00
0

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

Victor
  • 3,999
  • 3
  • 24
  • 27