I am creating this MVC3 Razor ASP.NET application in which I use a custom WebViewPage that inherits from this. Thus, all my views use my custom WebViewPage:
public class MyCustomWebViewPage : System.Web.Mvc.WebViewPage { public CustomHelper MyHelper { get; private set; } : }
And then in the web.config located in the Views folder, I pointed out that MyCustomViewPage is my default base page.
<system.web.webPages.razor> <pages pageBaseType="Namespace.MyCustomWebViewPage"> <namespaces> : </namespaces> </pages> </system.web.webPages.razor>
So far so good, on the View.cshtml pages I can access my custom helper from razor markup:
@MyHelper.SomeMethod()
Now there is something that I need to do conditionally on the assistant. I tried to set the helper property in the @ {} section of CSHTML startup, but it's too late to account for it.
So, I'm going to do this either on the called Action method, or override the OnActionExecuting () method.
The problem is that in the controller I did not find a way to access my user assistant, for example:
public class AnyController : Controller { public ActionResult Index() { MyHelper.SomeProperty = true; } }
To explain this a bit more, the concept is that in the action (action code in the controller) I want to set a property in the user assistant that will allow the use of certain functions. Thus, it does not appear on representations that it does not need. It differs from the @section concept, in which the content is defined in the view, but rather says: “I want this function to be added to the layout”, and the layout produced some predefined markup that would allow this function AND, which in several places in the view can be “nuggets”, which require “function support markup” or a script. If I use a section, this “general” script will be displayed several times, and this is not what I need.
Having said that, how can I access the (custom) helper defined on the base view page inside the controller?