📄 filedirectory.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.Text;
using System.Web;
using System.Runtime.Serialization;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Text.RegularExpressions;
using System.IO;
using Devspace.Commons.Tracer;
namespace Devspace.Ajax.Permutations {
public class FileDirectoryRewriter : IRewriter {
private HttpContext _context;
private Regex _xml = new Regex("xml");
private Regex _html = new Regex("html");
private Regex _text = new Regex("plain");
public FileDirectoryRewriter( HttpContext context) {
_context = context;
}
private bool DoesFileExistAndRewrite(string filename) {
string path = _context.Request.PhysicalPath + filename;
GenerateOutput.Write("DefaultRewriter.DoesFileExistAndRewrite", "Path (" + path + ")");
FileAttributes attributes;
try {
attributes = File.GetAttributes(path);
}
catch (FileNotFoundException) {
return false;
}
if ((attributes & FileAttributes.Directory) == 0) {
_context.RewritePath(filename);
return true;
}
else {
return false;
}
}
public virtual bool WriteRedirection(string mimetype) {
GenerateOutput.Write("DefaultRewriter.WriteRedirection", "Mimetype (" + mimetype + ")");
if (_xml.IsMatch(mimetype)) {
return DoesFileExistAndRewrite("default.xhtml");
}
if (_html.IsMatch(mimetype)) {
return DoesFileExistAndRewrite("default.html");
}
if (_text.IsMatch(mimetype)) {
return DoesFileExistAndRewrite("default.txt");
}
if (String.Compare(mimetype, "*/*") == 0) {
return DoesFileExistAndRewrite("content.html");
}
return false;
}
}
/// <summary>
/// FileDirectoryResource: Class used to implement the default permutations pattern.
/// The class also implements object pool functionality so that the object
/// instances can be retrieved from a pool instead of being instantiated
/// </summary>
public class FileDirectoryResource : URLRewriterBase {
public FileDirectoryResource(IRewriter rewriter) :
base( rewriter) {
}
public override bool IsResource(HttpRequest request) {
GenerateOutput.Write( "ASPNetResource.IsResource", "Path (" + request.PhysicalPath + ")");
GenerateOutput.Write( "ASPNetResource.IsResource", "URLPath (" + request.Path + ")");
FileAttributes attributes;
try {
attributes = File.GetAttributes(request.PhysicalPath);
}
catch (FileNotFoundException) {
return false;
}
if ((attributes & FileAttributes.Directory) != 0) {
return true;
}
else {
return false;
}
}
public override void WriteRepresentation(HttpRequest request) {
string[] elements = (string[])request.AcceptTypes.Clone();
Array.Sort(elements, new CompareMimeTypes());
Regex semiColon = new Regex(";");
foreach (string type in elements) {
String[] buffers = semiColon.Split(type);
if (_defaultRewriter.WriteRedirection(buffers[0])) {
break;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -