艾伟_转载:.NET重写URL浅谈

简介: 最近小项目要求重写url找了下资料用到了MS的2个dll,微软的例子写得太不明显了。后来终于改好了。ActionlessForm.dll------用来处理回发URLRewriter.dll----- 是微软封装好了的一个URL重写组件添加引用----具体的使用说明请去看http://msdn.microsoft.com/zh-cn/library/ms972974.aspx#XSLTsection123121120120比我说得好得多。

最近小项目要求重写url找了下资料用到了MS的2个dll,微软的例子写得太不明显了。后来终于改好了。

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.RewriterConfigSerializerSectio
Handler,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="_Defa
ult
" %>

<!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/xh
tml1/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;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Cover : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["id"] == null)
{
Response.End();
}
else
{
int id = Convert.ToInt32(Request.QueryString["id"]);
Response.Write(id);
}
}
}

 

还要去对iis设置:

这样的话伪静态就可以用了
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll ---这是上面的路径

浏览Default.aspx页

ListCategories.aspx --页面其实在服务器上面是没有的。它里面的内容是Default.aspx的内容,因为配置文件里面设置了

写得有点乱。

本例子是和微软的重写url基本一样的。算是简单化了一点点呵呵。

源代码

目录
相关文章
|
4月前
|
Web App开发 编解码 运维
LNMP详解(十二)——Nginx URL重写实战
LNMP详解(十二)——Nginx URL重写实战
47 2
|
16天前
|
开发框架 前端开发 .NET
Asp.net Webapi 的 Post 方法不能把参数加到 URL 中?试试这样写
Asp.net Webapi 的 Post 方法不能把参数加到 URL 中?试试这样写
|
4月前
|
开发框架 搜索推荐 中间件
中间件应用路由和URL重写
【5月更文挑战第2天】中间件应用路由和URL重写
32 3
中间件应用路由和URL重写
|
4月前
|
编解码 运维 搜索推荐
LNMP详解(十一)——Nginx URL重写原理
LNMP详解(十一)——Nginx URL重写原理
35 0
|
4月前
|
JavaScript Windows
VUE部署到IIS中报404错误解决方案-配置URL重写
VUE部署到IIS中报404错误解决方案-配置URL重写
185 0
|
12月前
|
iOS开发
解决alert在ios版微信中显示url的问题(重写alert)
解决alert在ios版微信中显示url的问题(重写alert)
86 0
|
移动开发
详解IIS中的重写工具下关于操作重定向URL中的{R:N}与{C:N}
详解IIS中的重写工具下关于操作重定向URL中的{R:N}与{C:N}
192 0
详解IIS中的重写工具下关于操作重定向URL中的{R:N}与{C:N}
|
应用服务中间件 nginx 开发者
Nginx反向代理/location/URL重写功能实战| 学习笔记
快速学习Nginx反向代理/location/URL重写功能实战。
205 0
Nginx反向代理/location/URL重写功能实战| 学习笔记
|
缓存 应用服务中间件 Shell
Nginx实战(二) URL重写
Nginx实战(二) URL重写
147 0
Go 语言入门很简单:net/url 包(下)
在 Golang 中,将 URL 打包用于从服务器获取数据非常重要。只需了解您是否正在处理任何应用程序并且您想从任何外部位置或服务器获取此应用程序的数据,都需要我们可以使用 URL。