All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
md5加密代码示例_[Asp.Net教程]
PHP入门:初学来看配置文件PHP.INI的中文注释_[PHP教程]
asp.net中的DataTable对象学习_[Asp.Net教程]
创建复杂表头的表格_[Asp.Net教程]
,2.0,中快速实现单点登陆_[Asp.Net教程]
用sql存储过程实现前台标题变色_[SQL Server教程]
C#教程:C#中的显示类型转换
网页制作技巧:设计干净的链接_[Html教程]
如何在十天学内会php之第四天_[PHP教程]
ASP.NET,3.5,Extensions新特性:ASP.NET,Dynamic,Data,体验1_[Asp.Net教程]
Reading number is top 10 pictures
餐桌上的鱼
NeedWallpaper11
做运动的校花2
Black and white also sexy--YanLiu1
The hot big eye big breast beauty2
A beautiful girl to bud1
A man's favorite things4
毛俊杰-能量永动机
开始让人感动,后来......
Summer is most suitable for young people to travel in China8
Download software ranking
Tram sex maniac 2 (H) rar bag10
美女游泳记
Unix video tutorial4
Unix video tutorial1
Be there or be square
Boxer's Top ten classic battle4
SP4 for SQL2000
终极变速大师Speeder3.26
艳兽都市
Boxer's Top ten classic battle2
delv published in(发表于) 2014/1/10 6:24:43 Edit(编辑)
asp.net,URL重写(URLRewriter),之简化版_[Asp.Net教程]

asp.net,URL重写(URLRewriter),之简化版_[Asp.Net教程]

asp.net URL重写(URLRewriter) 之简化版_[Asp.Net教程]

在 asp.net 里实现 URL重写(URLRewriter)的一个最简单的方法。


参考了 (作者 Scott Mitchell 翻译:Janssen )的大作,虽然没有完全看明白,但是也照猫画虎地做了一个,颇有“成就”感。写出来分享一下。


原作里讲了很多的原理,这里就不说了(其实我也不懂)。这里就写操作过程吧。目的是实现一个最简单的能实现 URL重写 的程序。


1、需要设置一下IIS里的站点属性。


2、修改web.config的内容。







其中*.zhtml 就是地址栏里面写的网页的扩展名,就是给用户看的,这个可以随意改(但是要符合扩展名的规则!)。当然要和第一步里面的设置相一致才行。


3、写一个类。


using System;
using System.IO;
using System.Web;
using System.Web.UI;


namespace ZDIL.URLRewriter
{
/**////


/// URL重写
///

public class RewriterFactoryHandler : IHttpHandlerFactory
{
/**////
/// GetHandler is executed by the ASP.NET pipeline after the associated HttpModules have run. The job of
/// GetHandler is to return an instance of an HttpHandler that can process the page.
///

/// The HttpContext for this request.
/// The HTTP data transfer method (GET or POST)
/// The RawUrl of the requested resource.
/// The physical path to the requested resource.
/// An instance that implements IHttpHandler; specifically, an HttpHandler instance returned
/// by the PageParser class, which is the same class that the default ASP.NET PageHandlerFactory delegates
/// to.

public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
{
string sendToUrl = url; //地址栏里面的地址
string filePath = pathTranslated;


string sendToURLString = "/web/index.aspx"; //真正要访问的页面
string queryString = ""; //参数。比如 ?id=123


filePath = context.Server.MapPath(sendToURLString); //物理地址


//这句最重要了。转向了。
context.RewritePath(sendToURLString, String.Empty, queryString);

//这个还没有弄明白 :)
return PageParser.GetCompiledPageInstance(url, filePath, context);
}


public virtual void ReleaseHandler(IHttpHandler handler)
{ //这个也不懂了
}
}
}


这个类呢,要写在一个单独的项目里面,然后编译成 ZDILURLRewriter.DLL文件。(注意文件名,写错了就不能正常运行了)。


4、完成了。


打开IE ,在地址栏里输入 http://.../1.zhtml。


浏览者看到是一个静态页的地址,但是实际上访问的却是 /web/index.aspx 这个动态网页。



怎么样简单吧。


当然了,这个是最简单的,简单到了“不能用”的地步了。因为他会把所有的 *.zhtml 的访问都“重写”到 /web/index.aspx 。


至于把什么样的网页重写到哪个网页,这里就不介绍了(这里只讲方法,不讲实现的细节)。


方法很多了,原作是通过正则来匹配的,我是通过 string sendToUrl = url; 来判断的。


其他的就看你们的需要了。


来源:网络







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.