I want to access a request parameter in (Razor) View. For example, bgColor sets the background color of the page. In View, I need to set Request["bgColor"] ?? "red". In contrast with RubyOnRails, where (in addition to the Request object) we have params hash with each request and that param hash is accessible in controllers as well as the rendered views for that request (params[:bgColor]), what would be the best ‘n sleek way to access a request parameter in Razor-View:
- Request[“paramName”]?
- Pushing it to ViewBag.bgColor in controller?
- Is there any parameter object/hash (like happen in RoR) which contains all the request params, whether they are captured in action’s parameter list or not?
In a properly designed ASP.NET MVC application views do not access anything else than the view model that it is passed to it from the controller action. A view should not try to pull data from Requests, Sessions, Cookies, Database, … A view is there to work with a view model. As far as background colors are concerned, writing a custom HTML helper that will look for some parameter in the request and generate corresponding markup seems like a good solution.