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}">
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}">
I think a more elegant solution is to use the controller and the ViewData dictionary:
If you are using .net core 2.0 this would be:
Sample usage:
For Asp.net Core 2
Noneof the answers worked for me, I was getting "'HttpRequestBase' does not contain a definition for 'Query'", but this did work:
It was suggested to post this as an answer, because some other answers are giving errors like 'The name Context does not exist in the current context'.
Just using the following works:
Sample usage:
Similar thread
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 currentHttpRequest
. Fastest way is by calling (as TruMan pointed out) `Request.Querystring' so the answer should be: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 fromContext.Request
path. So it would be:Please notice I am using
StringValues("1")
in the statement becauseQuery
returnsStringValues
struct instead of purestring
. That's cleanes way for this scenerio which I've found.