简体   繁体   English

用于声明客户端包含的ASP.NET指令约定

[英]ASP.NET Directive Convention for Declaring Client-Side Includes

I am loading .aspx and .ascx files as StreamReader . 我正在将.aspx和.ascx文件作为StreamReader加载。

I want each file to register it's javascript and stylesheet dependencies in some declaration like a <%@ ClientDependency path="~/Scripts/jquery-1.4.1.min.js" %> . 我希望每个文件在诸如<%@ ClientDependency path="~/Scripts/jquery-1.4.1.min.js" %>类的声明中注册其javascript和样式表依赖项。

Is there an existing convention for doing such a thing? 是否存在执行此类操作的现有约定? I don't need an implementation, but I don't want to create a new syntax if there is already a way to do it. 我不需要实现,但是如果已经有了一种新的语法,我就不想创建一个新的语法。

Also, what are the guidelines for custom <%@ blocks in ASP.NET? 另外,在ASP.NET中自定义<%@块的准则是什么?

Also, please retag this question if you can think of a more appropriate description. 另外,如果您可以想到更合适的描述,请重新标记该问题。

Basically... 基本上...

Here's what I'm doing in my current MVC project: 这是我在当前的MVC项目中正在做的事情:

  • Using Telerik's Web Asset Manager to register all of my common scripts and CSS in the Master Page that I know will be used on every page (There's an open source download, if like me, you didn't know) . 使用Telerik的Web Asset Manager在我知道的母版页中注册我所有的通用脚本和CSS,这将在每个页面上使用(有一个开源下载,如果像我一样,你可能不知道)

  • I encapsulate all JS functionality (beyond a few lines) into functions stored in appropriate external JS files. 我将所有JS功能(除了几行之外)封装到存储在适当的外部JS文件中的功能中。

  • I encapsulate all of my extended UI functionality into HTML Helpers. 我将所有扩展的UI功能都封装到HTML Helpers中。 They then use the StyleSheetRegistrar and ScriptsRegistrars to dynamically load any extra CSS or JS that are required for that particular helper. 然后,他们使用StyleSheetRegistrar和ScriptsRegistrars动态加载该特定帮助程序所需的任何其他CSS或JS。 And rely on the Telerik manager not to load them more than once. 并且依靠Telerik管理器不要多次加载它们。

  • If I ever use inline styles, I specify the style tag manually inside the Helper method via the htmlAttributes object, but its rare. 如果我曾经使用过内联样式,我会通过htmlAttributes对象在Helper方法内手动指定样式标签,但这很少见。

  • In order to call my javascript functionality needed on the HTML element, I use ScriptRegistrar().OnDocumentReady() 's string overload to specify a javascript function to call for that element. 为了调用HTML元素上所需的javascript功能,我使用ScriptRegistrar()。OnDocumentReady()的字符串重载来指定要对该元素调用的javascript函数。

  • I've also defined a simple extension method for ScriptRegistrarBuilder in my HtmlHelpers class that allows me to provide a parameter list of arguments to pass to my javascript method call, when calling OnDocumentReady() , without a heap of string concatenation. 我还在HtmlHelpers类中为ScriptRegistrarBuilder定义了一个简单的扩展方法,该方法允许我提供参数列表,以在调用OnDocumentReady()时传递给我的javascript方法调用,而不会产生大量字符串连接。

     public static void OnDocumentReady(this ScriptRegistrarBuilder builder, string format, params string[] args) { builder.OnDocumentReady(String.Format(format, args)); } 
  • And of course, I render all the JS at the bottom of the Master page. 当然,我将所有JS呈现在“母版”页面的底部。

You don't avoid all uses of inline JS here, but it keeps it pretty thin, if you do it right. 您不会在这里避免内联JS的所有使用,但是如果操作正确的话,它会使它变薄。 Honestly, I can't really think of another way that wouldn't start to get pretty over complicated. 老实说,我真的想不出另一种不会变得复杂的方法。

In the end I think you get something pretty flexible which at the same time allows you to be fairly specific about loading what you do and do not need on any given page. 最后,我认为您会获得一些相当灵活的东西,同时,您可以在任何给定页面上非常具体地加载要执行的操作和不需要的操作。

The only caveat I've found with this method is in terms of CSS. 我发现此方法的唯一警告是CSS。 In order to be able to dynamically load CSS files into the page when a helper needs them, I've had to render all the CSS includes at the bottom of the Master page, past any possible calls to add new files. 为了能够在助手需要它们时将CSS文件动态加载到页面中,我不得不在添加任何新文件的可能调用之后,将所有CSS包含内容呈现在“母版”页面的底部。

Haven't found a way around this yet, unfortunately. 不幸的是,尚未找到解决方法。

A small example 一个小例子

Perhaps needlessly detailed for you... but this way someone searching can hopefully find a useful solution too! 也许为您提供了不必要的详细信息...但是通过这种方式,希望有人搜索也可以找到有用的解决方案! :) :)

In the master page: 在母版页中:

<%
    Html.Telerik().StyleSheetRegistrar().DefaultGroup(styles => styles
       .Add("~/Content/Site.css")
       .Add("~/Content/core.css")
    );

    Html.Telerik().ScriptsRegistrar().DefaultGroup(scripts => scripts
        .Add("~/Scripts/common.js")
        .Combine(true)
        .Compress(true)
        .CacheDurationInDays(30)
    );
%>

Then somewhere just above the body closing tag: 然后在body结束标记上方的某处:

<%:
   Html.Telerik().ScriptsRegistrar().Render();
   Html.Telerik().StyleSheetsRegistrar().Render();
%>

In your HTML helper: 在您的HTML帮助器中:

    public static MvcHtmlString AutoCompleteComboListFor<TModel, TProperty>(this HtmlHelper<TModel> Helper, Expression<Func<TModel, TProperty>> expression,
        SelectList List, string DataSource)
    {
        MvcHtmlString dropDown = SelectExtensions.DropDownListFor<TModel, TProperty>(Helper, expression, List);

        string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
        string id = Helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName);

        //Do whatever you want

        helper.Telerik().ScriptRegistrar().DefaultGroup(scripts => scripts
            .Add("~/Scripts/extended.js")
            );
        helper.Telerik().StyleSheetRegistrar().DefaultGroup(styles => styles
            .Add("~/Content/extended.css")
            );

        helper.Telerik().ScriptRegistrar().OnDocumentReady(@"MyExtendedFunction('{0}');", id);

        return dropDown;
    }

Simply calling that helper (or any other) will end in a tidy block at the end of the page containing all JS code, files and CSS files. 简单地调用该帮助程序(或其他任何帮助程序)将在页面末尾的整洁代码块中结束,其中包含所有JS代码,文件和CSS文件。

<link type="text/css" href="/Content/Site.css" rel="stylesheet"/>
<script type="text/javascript" src="/asset.axd?id=kAAAAB-LCAAAAAAABADsvQdgHEmWJSYvbcp7f0r1StfgdKEIgGATJNiQQBDswYjN5pLsHWlHIymrKoHKZVZlXWYWQMztnbz33nvvvffee--997o7nU4n99__P1xmZAFs9s5K2smeIYCqyB8_fnwfPyJ-8Uezjx597xd_tPro0Uevp3WxapuPRh-d82dL-uynf9E6r6-3d8f79N9P47vqo0c7v2Qk3-bv2nw5y2f2m91f8v1f8v3RR9OWvmzp27s_nV1mDcOlBpf06d7O7s743nj33u4BfTKl3u99ep9--ehRW6_zX_L_BAAA___9S_3qkAAAAA%3d%3d"></script>

<script type="text/javascript"> 
//<![CDATA[
jQuery(document).ready(function(){
myFunction('Foo');});
//]]>
</script>

There is no existing convention for registering client includes with a directive. 目前尚无用于注册包含指令的客户端的约定。 My solution was to create a custom server control instead and use the existing TemplateParser functionality to get the include data (which ended up being preferable to loading the file as a stream and parsing it myself). 我的解决方案是创建一个自定义服务器控件,并使用现有的TemplateParser功能来获取包含数据(最终比将文件作为流加载并自行解析更可取)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM