simpleetag.cs

来自「ajax patterns 这是关于ajax设计模式方面的原代码」· CS 代码 · 共 84 行

CS
84
字号
/*
    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.Web;


namespace Devspace.Ajax.CacheController {
    public abstract class ETagBase : IHttpHandler{
        public ETagBase() { }
        protected internal abstract string GetETagValue();
        protected internal abstract void GenerateContent( HttpContext context);
        
        public void ProcessRequest( HttpContext context) {
            HttpRequest req = context.Request;
            HttpResponse resp = context.Response;
            if (req.HttpMethod == "GET") {
                try {
                    string sentETag = req.Headers.Get( "If-None-Match");
                    string localETag = GetETagValue();
                    if (sentETag != null && sentETag.CompareTo( localETag) == 0) {
                        resp.StatusCode = 304;
                        resp.Status = "304 Not modified";
                        return;
                    }
                    resp.AddHeader("ETag", localETag);
                    GenerateContent( context);
                }
                catch (Exception ex) {
                    throw new Exception("ETagBase generated error", ex);
                }
            }
        }
        public bool IsReusable {
            get {
                return false;
            }
        }
    }
    
    public class ETagExample1 : ETagBase {
        public ETagExample1() { }
        protected internal override String GetETagValue() {
            return "3e86-410-3596fbbc";
        }
        
        protected internal override void GenerateContent(HttpContext context) {
            HttpRequest req = context.Request;
            HttpResponse resp = context.Response;
            resp.ContentType = "text/html";
            resp.Output.Write( "<b>First Content</b>");
        }
    }
    public class ETagExample2 : ETagBase {
        public ETagExample2() { }
        
        protected internal override String GetETagValue() {
            return "3e86-410-3596fcbc";
        }
        
        protected internal override void GenerateContent(HttpContext context) {
            HttpRequest req = context.Request;
            HttpResponse resp = context.Response;
            resp.ContentType = "text/html";
            resp.Output.Write( "<b>Second Content</b>");
        }
    }
    
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?