asp.net url重写浅谈
2024-07-10 12:38:32
供稿:网友
ActionlessForm.dll------用来处理回发
URLRewriter.dll----- 是微软封装好了的一个URL重写组件
添加引用----
具体的使用说明请去看
http://msdn.microsoft.com/zh-cn/library/ms972974.aspx#XSLTsection123121120120
比我说得好得多。。
具体使用方法:
首先web.config的配置:
代码如下:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler,
URLRewriter" />
</configSections>
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/ListCategories/.aspx</LookFor>
<SendTo>~/Default.aspx</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/(/d+)/.html</LookFor>
<SendTo>~/Cover.aspx?id=$1</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
<system.web>
<httpModules>
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/>
</httpModules>
<compilation debug="true"/>
</system.web>
</configuration>
主要配置的代码是这些。其他的根据自己的需要和.net的版本自行添加。
然后Default.aspx,Cover.aspx,新建2个页面
Default.aspx:
代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="ListCategories.aspx">ListCategories.aspx</a>
<a href="30.html">30.html</a>
</div>
</form>
</body>
</html>
Cover.aspx:
代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Cover.aspx.cs" Inherits="Cover" %>
<%@ Register TagPrefix="skm" Namespace="ActionlessForm" Assembly="ActionlessForm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Cover</title>
</head>
<body>
<skm:form id="form1" runat="server">
<div>
Cover页面
<h4><a href="javascript:void(0)" onclick="history.go(-1)">返回上一页</a></h4>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</skm:form>
</body>
</html>
Cover.aspx.cs:
代码如下:
using System;
using System.Collections;
using System.Configuration;