How to get query string parameter from MVC Razor markup?

C#asp.netasp.net MvcRazorasp.net Mvc-4

C# Problem Overview


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}">

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

If you are using .net core 2.0 this would be:

Context.Request.Query["id"]

Sample usage:

<a href="@Url.Action("Query",new {parm1=Context.Request.Query["queryparm1"]})">GO</a>

Solution 3 - C#

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:

Request.Query["queryparm1"]

Sample usage:

<a href="@Url.Action("Query",new {parm1=Request.Query["queryparm1"]})">GO</a>

Solution 4 - C#

Noneof the answers worked for me, I was getting "'HttpRequestBase' does not contain a definition for 'Query'", but this did work:

HttpContext.Current.Request.QueryString["index"]

Solution 5 - C#

for ASP.NET Core 5.0

You can get query parameters by injecting IHttpContextAccessor into the Razor page. And get the value of any parameter with Request.Query object.

Sample URL => https://localhost:44326/?MyParam=MyValue

Index.cshtml:

@inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor

<span>@HttpContextAccessor.HttpContext.Request.Query["MyParam"][0]</span>

Note that there maybe several query parameters with the same name therefore the values are stored in a collection.

Solution 6 - C#

I think a more elegant solution is to use the controller and the ViewData dictionary:

//Controller:
public ActionResult Action(int IFRAME)
    {
        ViewData["IsIframe"] = IFRAME == 1;
        return View();
    }

//view
@{
    string classToUse = (bool)ViewData["IsIframe"] ? "iframe-page" : "";
   <div id="wrap" class='@classToUse'></div>
 }

Solution 7 - C#

For Asp.net Core 2

ViewContext.ModelState["id"].AttemptedValue

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTruMan1View Question on Stackoverflow
Solution 1 - C#MariuszView Answer on Stackoverflow
Solution 2 - C#Florian SchaalView Answer on Stackoverflow
Solution 3 - C#x5657View Answer on Stackoverflow
Solution 4 - C#Rolando RetanaView Answer on Stackoverflow
Solution 5 - C#Alper EbicogluView Answer on Stackoverflow
Solution 6 - C#YavgenyPView Answer on Stackoverflow
Solution 7 - C#Pradip RupareliyaView Answer on Stackoverflow