URL重写之实现IHttpHandler接口

2006年06月2日 5:08 下午  |  分类:Develop

以前用url重写时是用的ms urlrewriter,用了以后发现了很多不足,自定义功能太弱,而且随着重写规则的增加,web.config可能会越来越大,实际上,url重写就是实现IHttpHandler接口.
整个流程分二步走:
1、用一个xml文件来存储重写规则,其中这些规则是一些简单的正则表达式
2、实现IHttpHandler接口
首先看一下xml文件的格式:

<?xml version="1.0" encoding="utf-8" ?>
<root>
<regex>
<!--重写以后的虚拟地址-->
<b><![CDATA[xxx,(?<id>[0-9]+).html$]]></b>
<!--实际地址-->
<a><![CDATA[xxx.aspx?id=${id}]]></a>
</regex>
</root>

相信上面的xml大家都能看懂.

using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.VisualBasic;
//RegexInfo结构,用来存储从xml文件中读取到的数据
public struct RegexInfo
{
public string _before;
public string _after;
public RegexInfo(string before, string after)
{
_before = before.ToLower();
_after = after.ToLower();
}
}
//ipFilter结构,用来存储被封的IP
public struct ipFilter
{
public string _ip;
public ipFilter(string ip)
{
_ip = ip;
}
}
public class HtmlHttpHandler : IHttpHandler   //实现IHttpHandler接口
{

private List<RegexInfo> _regex_list = new List<RegexInfo>();
private List<ipFilter> _ip_filter = new List<ipFilter>();
public HtmlHttpHandler()
{
DataSet ds = new DataSet();
//读取url重写规则文件,并写入RegexInfo结构的实例中
ds.ReadXml(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Regexs.xml"));
foreach (DataRow r in ds.Tables["regex"].Rows)
_regex_list.Add(new RegexInfo(((string)r["b"]).Trim(), ((string)r["a"]).Trim()));
ds.Reset();
//读取被封的IP列表
ds.ReadXml(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/ipFilter.xml"));
foreach(DataRow r in ds.Tables["IpFilters"].Rows)
_ip_filter.Add(new ipFilter((string)r["ip"]));
}

public void ProcessRequest(HttpContext context)
{
string _ip = context.Request.UserHostAddress;   //获取IP
foreach (ipFilter r in _ip_filter)
{
if (_ip == r._ip)
{
context.Response.Write("对不起,您的IP:"+_ip+"已被限制!");
context.Response.End();
}
}
string path = context.Request.Path.ToLower();   //获取当前访问的重写过的虚假URL
foreach (RegexInfo r in _regex_list)
path = Regex.Replace(path, r._before, r._after);      //匹配出其真实的URL
context.Server.Execute(path);
}

// Override the IsReusable property.
public bool IsReusable
{
get { return true; }
}
}

OK,IHttpHandler接口就被实现了,下面稍配一下web.config就可以实现URL重写了
在web.config的<system.web></system.web>中加入

<httpHandlers>
<add verb=”*” path=”*.html” type=”HtmlHttpHandler”/>
</httpHandlers>

表示后缀名为.html的文件全部交给HtmlhttpHandler类去处理
最后配一下iis就行了
本blog的url重写就是这样实现的,至于简繁的转换,你可以加到ProcessRequest中,至于如何实现转换,到这个blog上找找吧:)

在vs2005中调用远程WebService

2006年06月2日 10:29 上午  |  分类:Develop

webservice提供一个接口,供我们调用,只需调用其相应的方法,就会有数据返回,数据是以xml格式返回的. 
有朋友问到:在我的blog中曾提到一些实用的Web Service,但不知道如何在vs中调用它,这里写了一个简单的教程.
希望  能有所帮助.  
几个实用的Web Service地址 
http://www.yibin001.com/2006/03/28/0000307.aspx  
这里我以天气预报地址:http://www.wopos.com/webservice/weather.asmx  为例来说明.   
首先,我们在vs2005新建一个工程,这个工程可以是一个web工程,也可以是一个win程序.  
当然,你也可以在vs2003中做下面的操作.我这里是建立的一个web工程   
选取asp.net网站,然后确定  这时,vs2005会自动为我们新建一个default.aspx并已经打开了该文件  
我们就在这个文件中来操作它 

下一步,我们选中网站,单击右键,选择添加web引用,这样就可以引用外部的web service了,这是最关键的一步.  
在下面的对话框中输入远程的web service地址,如:http://www.wopos.com/webservice/weather.asmx  
再点击&quot;前往&quot;,vs会自动搜索该web service上的所有方法   
web引用名相当于Namespace,你可以作更改,也可以用默认的,一般默认的引用名是域名的反写,vs还会告诉我们在这里找到了多少个方法 等有用的信息 
 
然后单击&quot;添加引用&quot;,这时我们的工程下就会多出一个名为&quot;App_WebReferences&quot;的目录(vs2003下可能会有所不同), 
 
这时,表明我们已经引用了外部的web service,我们在代码区可以像使用一般的类那样来调用它.现在转到代码区 当我敲入com时,vs已经提示我了 
 
创建一个Weather类的实例,然后调用getWeather(string city)方法就行了  
最终的代码 

 
 protected void Page_Load(object sender, EventArgs e)   
  {        
 com.wopos.www.Weather service = new com.wopos.www.Weather();      
   Response.Write(service.getWeather(&quot;武汉&quot;));    
 } 

 
现在编译预览
 
 OK!已经取到数据了,但格式并不是我们需要的,所以需要进一步处理,如何处理,请大家参考&quot;几个实用的Web Service&quot;中的相关说明,这里就不多说了.  
由此可见,调用一个web service是非常简单的,但是,如果远程的web service的地址做了更改,或是改变了方法名,那我们的web service就要重新引用了并编译了, 所以最好是把方法名和service URL写在配置文件中,这样就方便多了.

SQL智能完成工具

2006年05月28日 5:14 下午  |  分类:未分类


SQL Prompt™
针对SQL Server的智能感知工具。

SQL Prompt 为Microsoft SQL Server 编辑器提供一种智能感知形式的自动完成功能,当你正在写你自己的SQL命令时,它也会告诉你应该使用正确的格式,帮助你快速地写出格式良好的SQL语句。SQL Prompt会提升你创建任何SQL脚本的生产效率。

特点:

* 表/视图名称完成
* 列名称完成
* 存储过程名称完成
* USE 完成
* JOIN/JOIN ON 完成
* 自动把关键字转换成大写
* 关键字后自动弹出

SQL Prompt 的支持情况:
1.Visual Studio 2005
2.Visual Studio 2003
3.SQL Server 2000企业管理器
4.Microsoft Query Analyzer
5.UltralEdit32
6.EditPlus
r2005也是支持的(它的官方网站上没有写,但我试了下是可以的)。

请去官方网站免费下载吧!!
http://www.red-gate.com/products/SQL_Prompt/index.htm

点击下载该压缩文档

出處:
http://www.cnblogs.com/sifang2004/archive/2006/05/28/411271.html

用List类对文件进行分页?

2006年05月28日 9:30 上午  |  分类:Develop

看到BI上常有人问这样的问题,其实呢,解决的方法很简单
在asp中你可以用数组去解决
在.Net中你可以用List类或Quere类来解决
这里用.net来实现,其实也就是这个blog后台管理中的一个附件管理功能
因为是用文件操作,所以需要导入System.Io

public partial class FileBowser : System.Web.UI.Page
{

int _page = 1;   //设置当前页码为第1页
string _capture_string = "";   //用来存储主体的图片代码
int _pagesize = 30;   //每页显示的图片数
string _uploadPath = ConfigurationManager.AppSettings["UploadImagePhysicsDirectory"];  //文件夹的实路径
string _uploads = ConfigurationManager.AppSettings["Uploads"];   //虚路径
List<FileInfo> sl = new List<FileInfo>();  //.net 2.0中的泛类型,主要靠它了
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["page"] != null)
_page = Int16.Parse(Request.QueryString["page"]);
getFiles();
}

private void getFiles()
{
DirectoryInfo di = new DirectoryInfo(_uploadPath);
foreach (FileInfo fi in di.GetFiles())   //遍历当前目录下的所有文件,并添加至sl变量中
{
sl.Add(fi);
}
int index = 0;
int _startRecord = (_page-1) * _pagesize;  //计算起始数
if (sl.Count > 0)
{
//计算总页数
int _pagecount = sl.Count % _pagesize == 0 ? sl.Count / _pagesize : sl.Count / _pagesize + 1;

for (int i = _startRecord; i < _startRecord+_pagesize; i++)
{
if (index % 6 == 0) _capture_string += "<tr>";
_capture_string += string.Format("<td align=\"center\"><img src=\"../zh-cn/{0}/{1}\"", _uploads, sl[i].Name);
if (index % 6 == 5) _capture_string += "</tr>";

index++;
if (i >= (sl.Count-1)) break;

}
int quot = index % 6;
if (quot > 0)
{
for (; quot < 6; quot++) _capture_string += @"<td ></td>";
_capture_string += "</tr>";
}

}
else
{
_capture_string += "<tr><td>没有任何附件</td></tr>";
}
}
protected string getUploads()
{
return _capture_string;
}

}

前台用getUploads()方法即可得到分页,当然,分页的序号这里没有给出,留给大家了