I want to check the URL parameter in my Razor markup. For example, how do I do something like this:
<div id="wrap" class="@{if (URL "IFRAME" PARAMETER EQUALS 1) iframe-page}">
Generally, the query string is one of client-side state management techniques in ASP.NET in which query string stores values in URL that are visible to Users. We mostly use query strings to pass data from one page to another page in asp.net mvc. In asp.net mvc routing has support for query strings in RouteConfig.
The value of Request. QueryString(parameter) is an array of all of the values of parameter that occur in QUERY_STRING. You can determine the number of values of a parameter by calling Request. QueryString(parameter).
Similar thread
<div id="wrap" class=' @(ViewContext.RouteData.Values["iframe"] == 1 ? /*do sth*/ : /*do sth else*/')> </div>
EDIT 01-10-2014: Since this question is so popular this answer has been improved.
The example above will only get the values from RouteData
, so only from the querystrings which are caught by some registered route. To get the querystring value you have to get to the current HttpRequest
. Fastest way is by calling (as TruMan pointed out) `Request.Querystring' so the answer should be:
<div id="wrap" class=' @(Request.QueryString["iframe"] == 1 ? /*do sth*/ : /*do sth else*/')> </div>
You can also check RouteValues vs QueryString MVC?
EDIT 03-05-2019: Above solution is working for .NET Framework.
As others pointed out if you would like to get query string value in .NET Core you have to use Query
object from Context.Request
path. So it would be:
<div id="wrap" class=' @(Context.Request.Query["iframe"] == new StringValues("1") ? /*do sth*/ : /*do sth else*/')> </div>
Please notice I am using StringValues("1")
in the statement because Query
returns StringValues
struct instead of pure string
. That's cleanes way for this scenerio which I've found.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With