📄 configurationrewrite.cs
字号:
/*
Copyright 2006 Christian Gross http://www.devspace.com
From the book Ajax Patterns and Best Practices
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.Reflection;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using Devspace.Commons.Tracer;
namespace Devspace.Ajax.Permutations {
public class AssemblyLoader {
Assembly _assembly;
public AssemblyLoader(string path) {
_assembly = Assembly.Load(AssemblyName.GetAssemblyName(path));
}
public type Instantiate< type>(string typeidentifier) where type: class {
return _assembly.CreateInstance(typeidentifier) as type;
}
}
// This class is the start of a dynamic configuration base URL rewriter
public class ConfigurationRewrite : IURLRewriter {
HttpApplication _app;
HttpContext _context;
private const string constAssemblyPath = @"/home/cgross/ajax/patterns/website/ROOT/assemblies/devspace.ajax.dll";
private SortedList< string, string> _handlerIdentifiers = new SortedList< string, string>();
public ConfigurationRewrite( HttpApplication app) {
_app = app;
_context = app.Context;
_handlerIdentifiers.Add( "/cachecontroller/etagexample1", "Devspace.Ajax.CacheController.ETagExample1");
_handlerIdentifiers.Add( "/cachecontroller/etagexample2", "Devspace.Ajax.CacheController.ETagExample2");
_handlerIdentifiers.Add( "/persistentcommunications/globalstatus", "Devspace.Ajax.PersistentCommunications.GlobalStatus");
_handlerIdentifiers.Add( "/persistentcommunications/globalpresence", "Devspace.Ajax.PersistentCommunications.GlobalPresence");
_handlerIdentifiers.Add( "/persistentcommunications/globalserverpush", "Devspace.Ajax.PersistentCommunications.GlobalServerPush");
_handlerIdentifiers.Add( "/decouplednavigation/convertcase", "Devspace.Ajax.DecoupledNavigation.ConvertCase");
_handlerIdentifiers.Add( "/infinitedata/primenumbercalculator", "Devspace.Ajax.InfiniteData.PrimeNumberHandler");
_handlerIdentifiers.Add( "/restmvc/dotranslation", "Devspace.Ajax.RestMVC.Translation.TranslationHandler");
_handlerIdentifiers.Add( "/logging/message", "Devspace.Ajax.Logging.LogBuffer");
}
public bool IsResource(HttpRequest request) {
string type;
GenerateOutput.Write( "ConfigurationRewrite.IsResource", "Path (" + request.Path + ")");
if( _handlerIdentifiers.TryGetValue( request.Path, out type)) {
GenerateOutput.Write( "ConfigurationRewrite.IsResource", "Found a handler");
return true;
}
else {
GenerateOutput.Write( "ConfigurationRewrite.IsResource", "Checking if the URL (" + request.Path + ") is embedded");
foreach( KeyValuePair< string, string> value in _handlerIdentifiers) {
GenerateOutput.Write( "ConfigurationRewrite.IsResource",
"Key (" + value.Key + ") Value (" + value.Value + ")");
if( request.Path.Contains( value.Key)) {
GenerateOutput.Write( "ConfigurationRewrite.IsResource", "Found a handler that contains the URL");
return true;
}
}
GenerateOutput.Write( "ConfigurationRewrite.IsResource", "Handler could not be found");
return false;
}
}
public void WriteRepresentation(HttpRequest request) {
string type;
GenerateOutput.Write( "ConfigurationRewrite.WriteRepresentation", "Path (" + request.Path + ")");
if( !_handlerIdentifiers.TryGetValue( request.Path, out type)) {
foreach( KeyValuePair< string, string> value in _handlerIdentifiers) {
if( request.Path.Contains( value.Key)) {
type = value.Value;
break;
}
}
}
IHttpHandler handler = new AssemblyLoader( constAssemblyPath)
.Instantiate< IHttpHandler>( type);
if( handler != null) {
GenerateOutput.Write( "ConfigurationRewrite.WriteRepresentation", "Executing handler");
handler.ProcessRequest( _context);
_app.CompleteRequest();
}
else {
GenerateOutput.Write( "ConfigurationRewrite.WriteRepresentation", "Handler could not be executed");
}
}
}
}
/*IHttpHandler IsMagicFile( string mimeType, string filename) {
if( filename == ".aspx") {
string type = "";
bool retval = false;
if( _context.Request.Path == "/cachecontroller/etagexample1") {
type = "Devspace.Ajax.CacheController.ETagExample1";
retval = true;
}
else if( _context.Request.Path == "/cachecontroller/etagexample2") {
type = "Devspace.Ajax.CacheController.ETagExample2";
retval = true;
}
if( retval) {
GenerateOutput.Write("SlashPeriodRewriter.IsMagicFile",
"Ooowee magic file (" + _context.Request.Path + ") mime (" + mimeType +
") filename (" + filename + ")");
return new AssemblyLoader(constAssemblyPath).Instantiate<IHttpHandler>(type);
}
}
return null;
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -