Question

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

Was it helpful?

Solution 2

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

OTHER TIPS

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top