QueryString and RenderAction
-
14-04-2021 - |
سؤال
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
المحلول 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
نصائح أخرى
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);
}