What's the best method to be overloaded in ViewPage for inserting some script to all ViewPage?

Posted by Soul_Master on Stack Overflow See other posts from Stack Overflow or by Soul_Master
Published on 2009-10-20T11:18:12Z Indexed on 2010/03/09 2:06 UTC
Read the original article Hit count: 442

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about asp.net-mvc