c# razor url parameter from view

asp.net MvcUrlRazor

asp.net Mvc Problem Overview


Why does Request["parameterName"] returns null within the view? I know I can get it from the controller but I have to make a little check in the View. I am using ASP.NET MVC 3.

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

You can use the following:

Request.Params["paramName"]

See also: https://stackoverflow.com/questions/5706/when-do-request-params-and-request-form-differ

Solution 2 - asp.net Mvc

I've found the solution in this thread

@(ViewContext.RouteData.Values["parameterName"])

Solution 3 - asp.net Mvc

@(ViewContext.RouteData.Values["parameterName"])

worked with ROUTE PARAM.

Request.Params["paramName"]

did not work with ROUTE PARAM.

Solution 4 - asp.net Mvc

If you're doing the check inside the View, put the value in the ViewBag.

In your controller:

ViewBag["parameterName"] = Request["parameterName"];

It's worth noting that the Request and Response properties are exposed by the Controller class. They have the same semantics as HttpRequest and HttpResponse.

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
QuestionShaokanView Question on Stackoverflow
Solution 1 - asp.net MvcWouter SimonsView Answer on Stackoverflow
Solution 2 - asp.net MvcDanielView Answer on Stackoverflow
Solution 3 - asp.net MvcRavi RamView Answer on Stackoverflow
Solution 4 - asp.net MvcJamie DixonView Answer on Stackoverflow