博客 (106)

现在浏览器一般都支持gzip,deflate压缩协议 , 也就是说当服务器返回的是用gzip或deflate协议进行压缩过的内容, 浏览器将自动的进行解压缩 .这样做可以节省大量的网络带宽,负面影响是加重了服务器的负担.

我们只是对aspx页面进行压缩 ,当然也可以压缩js和css . 但你也想用来对图片也进行压缩的话就错了 ,效果和用winzip压缩图片一样, 只能增大体积.

首先来看看一个实例 aspx页面压缩前和压缩后的页面信息
压缩前

压缩后

可以看到压缩到原来页面大小的27% 效果还是可以的.
看看具体代码
CompressionModule

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->#region Using

using System;
using System.Web;
using System.IO.Compression;

#endregion

namespace BlogEngine.Core.Web.HttpModules
{
  /// <summary>
  /// Compresses the output using standard gzip/deflate.
  /// </summary>
  public class CompressionModule : IHttpModule
  {

    #region IHttpModule Members

    /// <summary>
    /// Disposes of the resources (other than memory) used by the module 
    /// that implements <see cref="T:System.Web.IHttpModule"></see>.
    /// </summary>
    void IHttpModule.Dispose()
    {
      // Nothing to dispose; 
    }

    /// <summary>
    /// Initializes a module and prepares it to handle requests.
    /// </summary>
    /// <param name="context">An <see cref="T:System.Web.HttpApplication"></see> 
    /// that provides access to the methods, properties, and events common to 
    /// all application objects within an ASP.NET application.
    /// </param>
    void IHttpModule.Init(HttpApplication context)
    {
      if (BlogSettings.Instance.EnableHttpCompression)
        context.BeginRequest += new EventHandler(context_BeginRequest);
    }

    #endregion

    #region Compression

    private const string GZIP = "gzip";
    private const string DEFLATE = "deflate";

    /// <summary>
    /// Handles the BeginRequest event of the context control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    void context_BeginRequest(object sender, EventArgs e)
    {
      HttpApplication app = sender as HttpApplication;
      //压缩aspx页面
      if (app.Request.Url.OriginalString.ToUpperInvariant().Contains(".aspx"))
      {
        //是否支持压缩协议
        if (IsEncodingAccepted(DEFLATE))
        {
          app.Response.Filter = new DeflateStream(app.Response.Filter, CompressionMode.Compress);
          SetEncoding(DEFLATE);
        }
        else if (IsEncodingAccepted(GZIP))
        {
          app.Response.Filter = new GZipStream(app.Response.Filter, CompressionMode.Compress);
          SetEncoding(GZIP);
        }
      }
    }

    /// <summary>
    /// Checks the request headers to see if the specified
    /// encoding is accepted by the client.
    /// </summary>
    private static bool IsEncodingAccepted(string encoding)
    {
      return HttpContext.Current.Request.Headers["Accept-encoding"] != null && HttpContext.Current.Request.Headers["Accept-encoding"].Contains(encoding);
    }

    /// <summary>
    /// Adds the specified encoding to the response headers.
    /// </summary>
    /// <param name="encoding"></param>
    private static void SetEncoding(string encoding)
    {
      HttpContext.Current.Response.AppendHeader("Content-encoding", encoding);
    }

    #endregion

  }
}
5,788
搜索引擎,对页面各个元素的权重比例Inbound link text: 10 points. 中文版:
外部链接文字:10分
标题:10分
域名:7分
H1,H2字号标题:5分
每段首句:5分
路径或文件名:4分
相似度(关键词堆积):4分
每句开头:1.5分
加粗或斜体:1分
文本用法:1分
title属性:1分
alt标记:0.5分
Meta描述(Description):0.5分
Meta关键词(Keywords):0.05分
3,647

我们在用数据控件的时候,一般会在 .aspx 页面生成如单向绑定

Text='<%# Eval("title") %>'

 

或双向绑定

Text='<%# Bind("title") %>'

 

之类的绑定代码,如果我们必需对它进行进一步处理,譬如取得的“产品分类的ID”,我想显示为“产品分类的名称”,或者格式化时间显示

等等。

如果只是连接字符串可以这样处理

Text='<%# Eval("title", "标题为:{0}") %>'

 

如果是通过一个方法去改变绑定值,则可以:

Text='<%# Server.HtmlEncode(Eval("title","{0}")) %>'

 

这里可以套用任何自定义的方法。更可以实现绑定两个数据库字段的实现:

Text='<%# formatUrl(Eval("ID","{0}"),Eval("xArchive","{0}")) %>'
public string formatUrl(string id, string archive)
{

if (archive.Trim() != "")
{

return "/xsdn/archive/" + archive + ".aspx";
}

else
{
return "/xsdn/"+id+".aspx";
}
}

 

 

xoyozo 17 年前
5,194

推荐参考更新的文章:.NET 5 / ASP.NET Core / ASP.NET Frameworks 从纯真 IP 数据库(QQWry.Dat)中查询 IP 归属地

纯真IP数据库官方下载地址:http://www.cz88.net/

之前我写过将 QQWry.Dat 转为 Access 格式后,在 ASP.NET 中通过简单算法去查询,缺点是 IP 数据库体积变大。现在有了更简单的方法:

1,下载 IPLocation.dll备用地址),将它放在网站的 Bin 目录下,将 QQWry.Dat 放在网站的任何位置(一般为 App_Data 目录)。刷新解决方案管理器。

2,在使用的页面加入引用

using IPLocation;

 

或者在 web.config 文件中,节点 pages -> controls 添加

<add tagPrefix="IPLocation" namespace="IPLocation" assembly="IPLocation"/>

 

3,调用方法:

Label1.Text = IPLocation.IPLocation.IPLocate(Server.MapPath("~/App_Data/QQWry.Dat"), TextBox1.Text.Trim());

 

方法 IPLocate() 参数一为 QQWry.Dat 路径,参数二为 IP 地址,返回 IP 归属地。

够简单吧,补充一点,在 VS2003 需要在项目中添加 IPLocation.dll 的引用。

xoyozo 17 年前
6,273
MasterPage.master
UserControl_OnTop.ascx
Page.aspx
UserControl_InPage.ascx
UserControl_OnButtom.ascx

MasterPage UserControlOnTop Page UserControlInPage UserControlOnButtom
  Init      
      Init  
        Init
Init        
    Init    
    Load    
Load        
  Load      
      Lod  
        Load
ControlEvents ControlEvents ControlEvents ControlEvents ControlEvents
    PreRender    
PreRender        
  PreRender      
      PreRender  
        PreRender
  UnLoad      
      UnLoad  
        UnLoad
UnLoad        
    UnLoad    
5,552

许多情况下,asp.net 在 Page_Load 事件中需要动态生成控件,这对于一些新手,包括我在内,会因为回传控件消失,或重复叠加控件等原因搞得头大,经过我翻阅资料和自己的实践,以博客评论页为例,把整个框架写在下面,仅供参考。

<asp:Panel>
<asp:Panel 记录索引 3 >
<asp:Label 昵称 /> <asp:Label 时间 /> <asp:LinkButton 支持按钮_3 /> <asp:LinkButton 删除按钮_3 /> 1楼
</asp:Panel>
<asp:Panel 记录索引 5 >
<asp:Label 昵称 /> <asp:Label 时间 /> <asp:LinkButton 支持按钮_5 /> <asp:LinkButton 删除按钮_5 /> 2楼
</asp:Panel>
</asp:Panel>


记录索引是指数据库中的自动编号。

支持按钮触发事件 LinkButton_support_Click(object sender, EventArgs e)

删除按钮触发事件 LinkButton_del_Click(object sender, EventArgs e)

所有子 Panel 和内部的所有控件都是在 Page_Load 事件中动态创建的,并且不能被嵌套在 if(!IsPostBack)

“点支持累计点击数”和“点删除删除一条记录”都是需要操作数据库的,我以前的做法是操作完数据库后 .Clear() 掉所有控件,再重新生成一次,这样既浪费服务器资源,又容易出现界面上的混乱,譬如显示了两遍该文章的评论。

其实上面代码中 Panel 套 Panel 的好处就是可以根据索引直接删除相应控件,然后直接在页面呈现,而省去上述烦恼。至于累计支持数就更简单了,下面的代码可以轻松解决问题:

string n = ((Label)Panel_commentLists.FindControl("Label_against_" + id)).Text;
n
= (Convert.ToInt32(n) + 1).ToString();
((Label)Panel_commentLists.FindControl(
"Label_against_" + id)).Text = n;

如果有修改和添加评论操作,同样道理,不要重复地全部 .Controls.Add 一次。为了添加时追加一个子 Panel 方便,建议在 Page_Load 的地方传参使用方法来做。

 

补充:在现在的开发中,我更喜欢用动态生成TableRow,TableCell来操作。

xoyozo 17 年前
4,897