110

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

7 Answers 7

169

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the hints. This worked: <div id="wrap" class="@(Request.QueryString["iframe"] == "1" ? "iframe" : string.Empty)">
I would add @TruMan1 's comment to your answer. In my case the values weren't in the route data (using nop-commerce) but were accessible via the request.
You might want to add that beginning with .NET Core 2.1 you need to change Context to HttpContext
That's working..Thanks. @Context.Request.Query["iframe"]
Going back to update multiple times is appreciated. Thanks for the effort.
|
46

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>

2 Comments

This gives 'The name Context does not exist in the current context'. Just using Request.Query["queryparm1"] works though.
@x5657 you should post your comment as an answer, as it was the only solution that really worked for me!
16

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>

1 Comment

That didn't work for me but this did: Request.QueryString
14

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.

2 Comments

don't forget to add nuget Microsoft.AspNetCore.Http
this worked for me even without Microsoft.AspNetCore.Http in .net core 7.0 core web api
11

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

Comments

5

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

2 Comments

This view is shared across many controllers. I think this would get messy doing it like this instead of putting the logic directly in the Razor markup. Also I anticipate I may have to do this in partial views as well.
A more elegant solution is to return it as part of a ViewModel return View(new SomeViewModel{ IsIFrame = IFrame });
1

For Asp.net Core 2

ViewContext.ModelState["id"].AttemptedValue

1 Comment

I'll just add that although it says AttempValue - if the paramter does not exist in the query string you will get an exception so you should first check: @if (ViewContext.ModelState["id"] != null)...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.