简体   繁体   English

将ASP.NET服务器脚本添加到大多数静态.JS / .CSS文件而不会丢失IntelliSense?

[英]Add ASP.NET server script to mostly-static .JS / .CSS files without losing IntelliSense?

Using VS2008 and ASP.NET 3.5 (or VS 2010 / .NET 4.0?), how can I include a bit of dynamic ASP.NET server-side code in mostly-static JavaScript and CSS files? 使用VS2008和ASP.NET 3.5(或VS 2010 / .NET 4.0?),如何在大多数静态JavaScript和CSS文件中包含一些动态ASP.NET服务器端代码?

I want to do this to avoid cloning entire JS or CSS files to vary just a small part of them multi-tenant sites. 我想这样做是为了避免克隆整个JS或CSS文件,只改变其中一小部分多租户网站。 Later, I want to extend the solution to handle localization inside javascript/CSS, dynamic debugging/tracing support, and other cool things you can get by injecting stuff dynamically into JavaScript and CSS. 后来,我想扩展解决方案来处理javascript / CSS中的本地化,动态调试/跟踪支持,以及通过将内容动态注入JavaScript和CSS可以获得的其他很酷的东西。

The hard part is that I don't want to lose all the cool things you get with static files, for example: 困难的是我不想丢失静态文件所带来的所有酷事,例如:

  • JS/CSS code coloring and intellisense JS / CSS代码着色和智能感知
  • CSS-class "go to definition" support in the IDE IDE中的CSS类“转到定义”支持
  • automatic HTTP caching headers based on date of underlying file 基于底层文件的日期自动HTTP缓存标头
  • automatic compression by IIS IIS自动压缩

The server-side goodness of static files (eg headers/compression) can be faked via an HttpHandler, but retaining IDE goodness (intellisense/coloring/etc) has me stumped. 静态文件(例如标题/压缩)的服务器端优点可以通过HttpHandler伪造,但保留IDE优点(intellisense / coloring / etc)让我感到难过。

An ideal solution would meet the following requirements: 理想的解决方案将满足以下要求:

  1. VS IDE provides JS/CSS intellisense and code coloring. VS IDE提供JS / CSS智能感知和代码着色。 Giving up server-code intellisense is OK since server code is usually simple in these files. 放弃服务器代码智能感知是可以的,因为服务器代码在这些文件中通常很简单。
  2. "go to defintion" still works for CSS classes (just like in static CSS files) “去定义”仍然适用于CSS类(就像在静态CSS文件中一样)
  3. send HTTP caching headers, varying by modified date of the underlying file. 发送HTTP缓存标头,根据底层文件的修改日期而变化。
  4. support HTTP compression like other static files 像其他静态文件一样支持HTTP压缩
  5. support <%= %> and <script runat=server> code blocks 支持<%=%>和<script runat = server>代码块
  6. URL paths (at least the ones that HTTP clients see) end with .JS or .CSS (not .ASPX). URL路径(至少是HTTP客户端看到的路径)以.JS或.CSS(不是.ASPX)结尾。 Optionally, I can use querystring or PathInfo to parameterize (eg choosing a locale), although in most cases I'll use vdirs for this. 或者,我可以使用querystring或PathInfo进行参数化(例如选择一个区域设置),尽管在大多数情况下我会使用vdirs。 Caching should vary for different querystrings. 缓存应根据不同的查询字符串而有所不同。

So far the best (hacky) solution I've come up with is this: 到目前为止,我提出的最佳(hacky)解决方案是:

  • Switch the underlying CSS or JS files to be .ASPX files (eg foo.css.aspx or foo.js.aspx). 将基础CSS或JS文件切换为.ASPX文件(例如foo.css.aspx或foo.js.aspx)。 Embed the underlying static content in a STYLE element (for CSS) or a SCRIPT element (for JS). 将底层静态内容嵌入STYLE元素(对于CSS)或SCRIPT元素(对于JS)。 This enables JS/CSS intellisense as well as allowing inline or runat=server code blocks. 这使得JS / CSS智能感知以及允许内联或runat =服务器代码块。
  • Write an HttpHandler which: 写一个HttpHandler:
    • looks at the URL and adds .aspx to know the right underlying ASPX to call 查看URL并添加.aspx以了解要调用的正确底层ASPX
    • uses System.Net.HttpWebRequest to call that URL 使用System.Net.HttpWebRequest来调用该URL
    • strips out the containing STYLE or SCRIPT tags, leaving only the CSS or JS 剥离包含STYLE或SCRIPT标签,只留下CSS或JS
    • adds the appropriate headers (caching, content type, etc.) 添加适当的标头(缓存,内容类型等)
    • compresses the response if the client suports compression 如果客户端支持压缩,则压缩响应
  • Map *.CSS and *.JS to my handler. 将* .CSS和* .JS映射到我的处理程序。
  • (if IIS6) Ensure .JS and .CSS file extensions are mapped to ASP.NET (如果是IIS6)确保将.JS和.CSS文件扩展名映射到ASP.NET

I'm already using a modified version of Darick_c's HttpCompression Module which handles almost all of above for me, so modifying it to support the solution above won't be too hard. 我已经使用了Darick_c的HttpCompression模块的修改版本,它可以为我处理上述几乎所有内容,因此修改它以支持上述解决方案并不会太难。

But my solution is hacky. 但我的解决方案是hacky。 I was wondering if anyone has a more lightweight approach for this problem which doesn't lose Visual Studio's static-file goodness. 我想知道是否有人有一个更轻量级的方法来解决这个问题,它不会失去Visual Studio的静态文件优点。

I know I can also hack up a client-side-only solution where I split all JS and CSS into "vary" and "won't vary" files, but there's a performance and maintenance overhead to this kind of solution that I'd like to avoid. 我知道我也可以破解一个仅限客户端的解决方案,我将所有的JS和CSS分成“变化”和“不会变化”的文件,但这种解决方案的性能和维护开销是我的喜欢避免。 I really want a server-side solution here so I can maintain one file on the server, not N+1 files. 我真的想要一个服务器端解决方案,所以我可以在服务器上维护一个文件,而不是N + 1个文件。

I've not tried VS10/.NET 4.0 yet, but I'm open to a Dev10/.net4 solution if that's the best way to make this scenario work. 我还没有尝试过VS10 / .NET 4.0,但我对Dev10 / .net4解决方案持开放态度,如果这是使这种情况有效的最佳方式。

Thanks! 谢谢!

I have handled a similar problem by having a master page output a dynamic generated JSON object in the footer of each page. 我通过让母版页在每个页面的页脚中输出动态生成的JSON对象来处理类似的问题。

I needed to have my js popup login dialog box support localization. 我需要让我的js popup登录对话框支持本地化。 So using JSON.NET for serialization, I created a public key/value collection property of the master page that pages could access in order place key/values into such as phrase key/localized phrase pairs. 因此,使用JSON.NET进行序列化,我创建了一个页面可以访问的母版页的公钥/值集合属性,以便将键/值放入短语键/本地化短语对中。 The master page then renders a dynamic JSON object that holds these values so that static js files could reference these dynamic values. 然后,母版页呈现一个动态JSON对象,该对象包含这些值,以便静态js文件可以引用这些动态值。

For the js login box I have the masterpage set the localized values. 对于js登录框,我让母版页设置了本地化的值。 This made sense because the masterpage also includes the login.js file. 这是有道理的,因为母版页还包含login.js文件。

I do commend you on your concern over the number of http requests being made from the client and the payload being returned. 我确实对您对从客户端发出的http请求数量以及返回的有效负载的关注表示赞赏​​。 Too many people I know and work with overlook those easy optimizations. 我认识和工作的人太多,忽略了那些简单的优化。 However, any time I run into the same issue you're having (which is actually quite often), I have found I've usually either taken a wrong turn somewhere or am trying to solve the problem the wrong way. 但是,任何时候我遇到你遇到的同一个问题(实际上经常是这样),我发现我通常要么在某个地方转错,要么试图以错误的方式解决问题。

As far as your JS question goes, I think Frank Schwieterman in the comments above is correct. 就你的JS问题而言,我认为Frank Schwieterman在上面的评论中是正确的。 I'd be looking at ways to expose the dynamic parts of your JS through setters. 我将研究如何通过setter公开JS的动态部分。 A really basic example would be if you have want to display a customized welcome message to users on login. 一个非常基本的例子是,如果您想要在登录时向用户显示自定义欢迎消息。 In your JS file, you can have a setMessage(message) method exposed. 在JS文件中,您可以公开setMessage(message)方法。 That method would then be called by the page including the script. 然后,包含脚本的页面将调用该方法。 As a result, you'd have something like: 结果,你会有类似的东西:

<body onLoad="setMessage('Welcome' + <%= user.FirstName %>);">

This can obviously be expanded by passing objects or methods into the static JS file to allow you the functionality you desire. 显然,可以通过将对象或方法传递到静态JS文件来扩展,以便为您提供所需的功能。

In response to the CSS question, I think you can gain a lot from the approach Shawn Steward from the comments makes a good point. 在回答CSS问题时,我认为你可以从Shawn Steward的评论中获得很多好处。 You can define certain static parts of your CSS in the base file and then redefine the parts you want to change in other files. 您可以在基本文件中定义CSS的某些静态部分,然后在其他文件中重新定义要更改的部分。 As a result, you can then dictate the look of your website by which files you're including. 因此,您可以根据您所包含的文件来决定您网站的外观。 Also, since you don't want to take the hit for extra http requests (keep in mind, if you set those files to be cached for a week, month, etc. it's a one time request), you can do something like combining the CSS files into a single file at compilation or runtime. 此外,因为您不想接受额外的http请求(请记住,如果您将这些文件设置为缓存一周,一个月等,这是一次性请求),您可以执行类似组合的操作CSS文件在编译或运行时编译成单个文件。

Something like the following links may be helpful in pointing you in the right direction: 类似以下链接可能有助于指明您正确的方向:

http://geekswithblogs.net/rashid/archive/2007/07/25/Combine-Multiple-JavaScript-and-CSS-Files-and-Remove-Overheads.aspx http://geekswithblogs.net/rashid/archive/2007/07/25/Combine-Multiple-JavaScript-and-CSS-Files-and-Remove-Overheads.aspx

http://www.asp.net/learn/3.5-SP1/video-296.aspx?wwwaspnetrdirset=1 http://www.asp.net/learn/3.5-SP1/video-296.aspx?wwwaspnetrdirset=1

http://dimebrain.com/2008/04/resourceful-asp.html http://dimebrain.com/2008/04/resourceful-asp.html

By utilizing the combining at run or compile time you can gain the best of both world by allowing you to logically separate CSS and JS files, yet also gaining the reduction of payload and requests that comes with compressing and combining files. 通过在运行或编译时利用组合,您可以通过允许逻辑上分离CSS和JS文件来获得两全其美,同时还可以减少压缩和组合文件带来的有效负载和请求。

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

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