📄 dynamiclistboxresourcehandler.cs
字号:
using System;
using System.Web;
using System.Reflection;
namespace MetaBuilders.WebControls {
/// <summary>
/// The DynamicListBoxResourceHandler takes care of script support for the controls in the assembly.
/// It is not meant to be used by your code.
/// </summary>
public class DynamicListBoxResourceHandler : System.Web.IHttpHandler {
#region IHttpHandler Members
/// <summary>
/// Processes the request by emitting script embedded in the assembly.
/// </summary>
/// <param name="context"></param>
void IHttpHandler.ProcessRequest(System.Web.HttpContext context) {
String scriptFile = context.Request.QueryString[resourceKey];
context.Response.ContentType = "text/javascript";
context.Response.Cache.SetExpires(DateTime.Now.AddMonths(1));
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetValidUntilExpires(false);
context.Response.Cache.VaryByParams[resourceKey] = true;
context.Response.Cache.VaryByParams[assemblyIdKey] = true;
context.Response.Write(GetScript(context.Cache, scriptFile));
}
bool IHttpHandler.IsReusable {
get {
return true;
}
}
private static readonly String handlerName = "MetaBuilders_WebControls_DynamicListBoxResourceHandler.axd";
private static readonly String resourceKey = "r";
private static readonly String assemblyIdKey = "c";
#endregion
#region Script
internal static void RegisterScript( System.Web.UI.Page page, String scriptKey, String scriptFile ) {
if ( !page.IsClientScriptBlockRegistered(scriptKey) ) {
String script = "";
if ( IsRegistered ) {
script = "<script language='javascript' type='text/javascript' src='" + page.ResolveUrl("~/" + GetScriptUrl(scriptFile) ) + "'></script>";
} else {
script = "<script language='javascript' type='text/javascript'>\r\n<!--\r\n" + GetScript(page.Cache, scriptFile) + "\r\n// -->\r\n</script>";
}
page.RegisterClientScriptBlock(scriptKey,script);
}
}
private static String GetScriptUrl(String scriptFile) {
Assembly a = typeof( DynamicListBoxResourceHandler).Assembly;
String assemblyId = System.IO.File.GetCreationTime( a.Location ).GetHashCode().ToString();
return handlerName + "?" + assemblyIdKey + "=" + assemblyId + "&" + resourceKey + "=" + scriptFile;
}
private static String GetScript(System.Web.Caching.Cache cache, String scriptFile) {
if ( cache == null ) {
return GetScriptDirect(scriptFile);
}
String key = "MetaBuilders WebControls DynamicListBox Script " + scriptFile;
if ( cache[key] == null ) {
cache.Insert(key,GetScriptDirect(scriptFile));
}
return (String)cache[key];
}
private static String GetScriptDirect(String scriptFile) {
if ( scriptFile == "DualList.js" || scriptFile == "DynamicListBox.js" ) {
using (System.IO.StreamReader reader = new System.IO.StreamReader(typeof(DynamicListBoxResourceHandler).Assembly.GetManifestResourceStream(typeof(DynamicListBoxResourceHandler), scriptFile))) {
return reader.ReadToEnd();
}
}
return "";
}
#endregion
#region Registration
private static Boolean IsRegistered {
get {
HttpContext context = HttpContext.Current;
if ( context == null ) {
return DetermineIsRegistered();
}
String cacheKey = "IHttpHandlerFactory Installed " + handlerName;
if ( context.Cache[cacheKey] == null ) {
context.Cache.Insert(cacheKey, DetermineIsRegistered());
}
return (Boolean)context.Cache[cacheKey];
}
}
private static Boolean DetermineIsRegistered() {
Object handlerMap = System.Web.HttpContext.GetAppConfig("system.web/httpHandlers");
if ( handlerMap == null ) {
return false;
}
MethodInfo findMapping = handlerMap.GetType().GetMethod("FindMapping", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic );
if ( findMapping == null ) {
return false;
}
Object handler = findMapping.Invoke(handlerMap, new Object[] { "GET", handlerName } );
if ( handler == null ) {
return false;
}
PropertyInfo handlerPathProperty = handler.GetType().GetProperty("Path", BindingFlags.NonPublic | BindingFlags.Instance );
if ( handlerPathProperty == null ) {
return false;
}
String handlerPath = handlerPathProperty.GetValue(handler,null) as String;
if ( handlerPath == null || handlerPath != handlerName ) {
return false;
}
return true;
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -