What's the best method to be overloaded in ViewPage for inserting some script to all ViewPage?
- by Soul_Master
I want to insert some script that is required for my JavaScript library in all view pages. I know that Asp.net MVC is built on Asp.net Framework. Therefore, I can override many methods in “System.Web.UI.Page” class that is a parent of “System.Web.Mvc.ViewPage” class.
Nevertheless, I can do it by override Render method but it makes all view pages be invalid for XHTML 1.0 strict. Correct JavaScript must be placed in header tag of HTML document.
public class ViewPage<TModel> : ViewPage where TModel : class
{
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
base.Render(writer);
writer.Write("<script type='text/javascript>applicationPath = window.applicationPath = 'somePath';</script>");
}
}
Thanks,
PS. I know I can create some code in each master page for doing that. However, it is a quite complicate for other developers to start using my JavaScript source code.