How to get query string parameter from MVC Razor markup?


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

public ActionResult Action(int IFRAME)
        ViewData["IsIframe"] = IFRAME == 1;
        return View();

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

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

Here's a sample method we wrote for doing something similar.

public static bool IsCurrentAction(this HtmlHelper helper, string actionName, string controllerName)
  string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
  string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

  return currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase));

In ASP.NET MVC 2 - How do I get Route Values into my navigation controller so I can highlight the current link?

You don't need to pass route data to a controller because the controller already has knowledge of it via the RouteData property:

public ActionResult Index() {
    // You could use this.RouteData here

Now if you want to pass some simple arguments like current action that was used to render the view you could do this:

<%= Html.RenderAction(
    new {
        currentStreamUrl = "Blog", 
        currentAction = ViewContext.RouteData.Values["action"],
        currentController = ViewContext.RouteData.Values["controller"]
); %>

And in your controller:

public ActionResult Nav(string currentAction, string currentController) 

I've found the solution in this thread


I've found the solution in this thread