关于请求的目录不存在而需要url重写

http://pwqzc.cnblogs.com/archive/2005/11/21/281331.html看到的讨论
其实csdn的那两个帖子
http://community.csdn.net/Expert/topic/4401/4401936.xml?temp=.317898

http://community.csdn.net/Expert/topic/4406/4406615.xml?temp=.513714
中已经有网友们说到方法了,hander module 都可以做
在iis中设置*.*映射到 aspnet_isapi.dll ISAPI 扩展,我测试后没有发现什么访问被拒绝访问的401.3错误,
需要输入用户名的问题。

r_isapi.gif

  

以下在代码xpsp2 .net1.1 sp1 下测试通过
[Copy to clipboard]
using System; using System.Web; using System.Web.UI; namespace AspxBoy.VisualUrl { /// <summary> /// VisualUrlHttpHander 的摘要说明。 /// </summary> public class VisualUrlHttpHander : IHttpHandlerFactory { #region IHttpHandlerFactory 成员 public void ReleaseHandler(IHttpHandler handler) { // TODO: 添加 VisualUrlHttpHander.ReleaseHandler 实现 } public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated) { // TODO: 添加 VisualUrlHttpHander.GetHandler 实现 string rawUrl = context.Request.RawUrl.Trim(); /// ///这里只示范地址是http://domainname/xxx ///和http://domainname/xxx/这样的地址 /// string userName = rawUrl.TrimStart('/').TrimEnd('/'); string sendToUrl = url; string filePath = pathTranslated; string sendToUrlLessQString; sendToUrl = Globals.AppPath + "/get.aspx?username=" + userName; Globals.RewriteUrl(context, sendToUrl, out sendToUrlLessQString, out filePath); return PageParser.GetCompiledPageInstance(sendToUrlLessQString, filePath, context); } #endregion } public class Globals { /// <summary> /// 应用程序路径 /// </summary> public static string AppPath { get { string applicationPath = "/"; if(HttpContext.Current != null) { applicationPath = HttpContext.Current.Request.ApplicationPath; } if (applicationPath == "/") { return string.Empty; } else { if (applicationPath.EndsWith("/")) { return applicationPath; } else { return applicationPath + "/"; } } } } /// <summary> /// 在ASP.NET中执行URL重写 /// see http://www.aspxboy.com/484/archive.aspx /// </summary> /// <param name="context"></param> /// <param name="sendToUrl"></param> /// <param name="sendToUrlLessQString"></param> /// <param name="filePath"></param> public static void RewriteUrl(HttpContext context, string sendToUrl, out string sendToUrlLessQString, out string filePath) { if (context.Request.QueryString.Count > 0) { if (sendToUrl.IndexOf('?') != -1) { sendToUrl += "&" + context.Request.QueryString.ToString(); } else { sendToUrl += "?" + context.Request.QueryString.ToString(); } } string queryString = String.Empty; sendToUrlLessQString = sendToUrl; if (sendToUrl.IndexOf('?') > 0) { sendToUrlLessQString = sendToUrl.Substring(0, sendToUrl.IndexOf('?')); queryString = sendToUrl.Substring(sendToUrl.IndexOf('?') + 1); } filePath = string.Empty; filePath = context.Server.MapPath(sendToUrlLessQString); context.RewritePath(sendToUrlLessQString, String.Empty, queryString); } } }

下载
http://files.cnblogs.com/huobazi/VisualUrl.rar

转载于:https://www.cnblogs.com/huobazi/archive/2005/11/21/NoDirUrlRewrite.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值