如何将JavaScript文件的链接添加到某个ASP .NET MVC页面的标题中
如何将JavaScript文件的链接添加到某个ASP .NET MVC页面的标题中?
可以说有_Layout.cshtml和About.cshtml,我需要将一些javascript文件放到About.cshtml的标题中。 我只是指那个页面。
http://www.dotnetcurry.com/ShowArticle.aspx?ID=636
怎么做?
为什么必须在标题中? 您可以包含内联所需的任何脚本:
我的解决方案
_Layout.cshtml
!DOCTYPE html> @ViewBag.Title @if (IsSectionDefined("AddToHead")) { @RenderSection("AddToHead", required: false) }
About.cshtml
@{ ViewBag.Title = "Index"; } Index
@section footer { Footer Here } @section AddToHead { }
试试下面的代码
HtmlGenericControl my_js = new HtmlGenericControl("script"); my_js.Attributes["async"] = "async"; my_js.Attributes["type"] = "text/javascript"; my_js.Attributes["src"] = "http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"; Page.Header.Controls.Add(my_js);
您需要使用命名空间:“使用System.Web.UI.HtmlControls”来使用HtmlGenericControl。