从上一篇文章,我们可以实现对域名后面的那部分进行重写,那么可不可以对前面那部分进行重写而实现二级域名呢?
答案是肯定的。
这样,首先我们得修改UrlRewriter,怎么修改请参见江大鱼的BLog。
1.BaseModuleRewriter.cs
{
HttpApplication app = (HttpApplication) sender;
Rewrite(app.Request.Path, app);
}
改为
{
HttpApplication app = (HttpApplication) sender;
Rewrite(app.Request.Url.AbsoluteUri, app);
}
就是将 app.Request.Path 替换成了 app.Request.Url.AbsoluteUri
2.ModuleRewriter.cs
{
// get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
// Create a regex (note that IgnoreCase is set)
Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
// See if a match is found
if (re.IsMatch(requestedPath))
{
// match found - do any replacement needed
string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
// log rewriting information to the Trace object
app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);
// Rewrite the URL
RewriterUtils.RewriteUrl(app.Context, sendToUrl);
break; // exit the for loop
}
}
改为
{
// get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
string lookFor = "^" + rules[i].LookFor + "$";
// Create a regex (note that IgnoreCase is set)
Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
// See if a match is found
if (re.IsMatch(requestedPath))
{
// match found - do any replacement needed
string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
// log rewriting information to the Trace object
app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);
// Rewrite the URL
RewriterUtils.RewriteUrl(app.Context, sendToUrl);
break; // exit the for loop
}
}
将
string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
改成了
string lookFor = "^" + rules[i].LookFor + "$";
完成这2处改动之后重新编译项目,将生成的dll复制到bin目录下。
修改完了这后,我们再把此 UrlRewriter.dll COPY 到我们项目的Bin目录下。这样就结了么?没有。
首先请确定你的项目之前有按我上篇文章中写到的那样做过UrlRewriter的配置,否则请先回过头来看看那篇文章。
如果你的项目已配置过,那么,我们还要为此做以下几件事情:
1。请确定你的域名是支持泛解析的。然后你的网站为默认网站,否则将不能实现(至少我现在还没有找到好办法)
2。在IIS配置:在IIS/你的站点/属性/主目录/配置/映谢 在通配符应用程序配置处插入一个新的映谢。把可执行文件设为和上面ASPX页面同样的配置即可(注意不要勾选 “确定文件是否存在”)。(用处就是使所有请求通过 asp.net 的ISAPI来处理,只有这样才能对所有地址进行重写嘛。)
3。查看下你的网站主机头,里面的第一个主机头值必须为空,否则会出现错误的请求。后面就随你加了,看你想绑定多少域名了。(这个办法是江大鱼想出来的。为这个错误我们都想了好多办法。在这里感谢江大鱼。。。)
4。最后改写你的 web.config 文件。
把上节中说到的
<httpHandlers>
<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
<add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
</httpHandlers>
改为:
<httpModules>
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
</httpModules>
(就是改用HTTP 模块来执行重写,而不用HTTP 程序,否则无法重写地址前面。)
然后就来修改我们的重写正则了:
<RewriterRule>
<LookFor>http://(.[0-9]*)/.178b2b/.com/</LookFor>
<SendTo>~/Search/Search_Sell.aspx?id=$1</SendTo>
</RewriterRule>
好了,现在你输入 http://1.178b2b.com/ 就能搜索出相应分类了。但是聪明的你马上就发现。晕死,首页进不去了。呵呵。当然喽。你还得为首页加入重写正则。
<RewriterRule>
<LookFor>http://www/.178b2b/.com/</LookFor>
<SendTo>~/index.htm</SendTo>
</RewriterRule>
大功告成。感觉爽死了吧。呵呵。莫急,如果你二级域名指向的目录下面的页面都用的相对地址连接的图片和其它页面的话,呵呵,你有得忙了,你要全部改成如下方式:
<a href=http://www.178b2b.com/cxlm/league.html target="_blank">诚信联盟</a>
以上就是用UrlRewriter实现二级域名的方法了。希望各位一切顺利。
最后,感谢江大鱼的无私帮助。
1. Application用来保存所有用户共用的信息
2. 在Asp时代,如果要保存的数据在应用程序生存期内不会或者很少发生改变,那么使用Application是理想的选择。但是在Asp.net开发环境中我们把类似的配置数据放在Web.config中。
3. 如果要使用Application 要注意的是所有的写操作都要在Application_OnStart事件中完成(global.Asax),尽管可以使用Application.Lock()避免了冲突,但是它串行化了对Application的请求,会产生严重的性能瓶颈。
4. 不要使用Application保存大数据量信息
5. 代码:Application[“UserID”]=”test”;
String UserName=Application[“UserID”].ToString();
Session
1. Session用来保存每一个用户的专有信息
2. Session的生存期是用户持续请求时间加上一段时间(一般是20分钟左右)
3. Session信息是保存在Web服务器内存中的,保存数据量可大可小
4. Session超时或者被关闭将自动释放数据信息
5. 由于用户停止使用应用程序之后它仍在内存中存留一段时间,因此这种方法效率较低
6. 代码:Session[“UserID”]=”test”;
String UserName=Session[“UserID”].ToString();
Cookie
1. Cookie用来保存客户浏览器请求服务器页面的请求信息
2. 我们可以存放非敏感的用户信息,保存时间可以根据需要设置
3. 如果没有设置Cookie失效日期,它的生命周期保存到关闭浏览器为止
4. Cookie对象的Expires属性设置为MinValue表示永不过期
5. Cookie存储的数据量受限制,大多数的浏览器为4K因此不要存放大数据
6. 由于并非所有的浏览器都支持Cookie,数据将以明文的形式保存在客户端
7. 代码:Resopnse.Cookies[“UserID”]=”test”;
String UserName= Resopnse.Cookies [“UserID”].ToString();
ViewState
1. ViewState用来保存用户的状态信息,有效期等于页面的生命周期
2. 可以保存大量数据但是要慎用,因为会影响程序性能
3. 所有的Web服务器控件都是用ViewState在页面PostBack期间保存状态
4. 不需要则关闭 @page 里面设置EnableViewState=false
5. 代码:ViewState[‘”ID”]=”yiner”;
String ID =ViewState[“ID”].ToString();
Cache
1. Cache用于在Http请求期间保存页面或者数据
2. Cache的使用可以大大的提高整个应用程序的效率
3. 它允许将频繁访问的服务器资源存储在内存中,当用户发出相同的请求后
服务器不是再次处理而是将Cache中保存的数据直接返回给用户
4. 可以看出Cache节省的是时间—服务器处理时间
5. Cache实例是每一个应用程序专有的,其生命周期==该应用程序周期
应用程序重启将重新创建其实例
6. 注意:如果要使用缓存的清理、到期管理、依赖项等功能必须使用Insert 或者Add方法方法添加信息
7. 代码:Cache[‘”ID”]=”yiner”;或者Cache.Insert(“ID”,”test”);
String ID =Cache[“ID”].ToString();
Hidden
1. Hidden控件属于Html类型的服务器控件,始终处于隐藏状态
2. 每一次提交的时候它会和其他服务器控件一起提交到服务器端
3. 代码如下:Hidden.Value=”king”;
string id=Hidden.Value; 要使用Runat=server