关于URL Rewrite的一个问题

HarrisonCao 2009-12-29 10:22:36
最近小弟正在做一个项目,项目中包含一个这样的功能,在用户访问网址 http://bbgang.com/hcao0 时,实际访问 http://bbgang.com/shoppages/shop.aspx?sid=1 , 我用HttpModule实现了该功能,用VS运行该功能时没有任何问题,但是发不到IIS时就出现无法访问的问题,请各位大侠指教!
...全文
131 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
happyboyxq1985 2009-12-30
  • 打赏
  • 举报
回复
服务器的IIS是不是没配制好啊
HarrisonCao 2009-12-30
  • 打赏
  • 举报
回复
现在是可以了 但是在IIS6.0中添加一个 .* 映射就提示扩展名错误
w362335821 2009-12-30
  • 打赏
  • 举报
回复
IIS 6 是不支持重写的,你需要下载一个重写的文件,这个网上很多
yilinkit 2009-12-30
  • 打赏
  • 举报
回复
IIS-网站属性-主目录-配置-应用程序配置,建立应用程序映射
jenny0810 2009-12-30
  • 打赏
  • 举报
回复
标记
sunxw18 2009-12-30
  • 打赏
  • 举报
回复
LZ可能没分清楚虚拟目录和网站,在VS里面运行的是虚拟目录,所以造成路径有问题
jshi123 2009-12-30
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 harrisoncao 的回复:]
http://bbgang.com/shoppages/shop.aspx?sid=8411fee9-6043-420b-b05e-30a656293c3d
[/Quote]
服务器不太稳定啊,好像罢工了
liaoyukun111 2009-12-30
  • 打赏
  • 举报
回复
真不会,学习下
骑猪看海 2009-12-30
  • 打赏
  • 举报
回复
可以重写Url,然后在web.config文件里配置下即可
HarrisonCao 2009-12-30
  • 打赏
  • 举报
回复

<!--配置文件-->
<add name="UrlReWrite" type="UrlProcess.UrlReWrite,UrlProcess"/>
HarrisonCao 2009-12-30
  • 打赏
  • 举报
回复
 public class UrlReWrite : IHttpModule
{
HttpApplication application = null;
#region IHttpModule 成员

public void Dispose()
{
//throw new NotImplementedException();
}

public void Init(HttpApplication context)
{
context.BeginRequest += new System.EventHandler(context_BeginRequest);
application = context;
}
private void context_BeginRequest(object sender, System.EventArgs e)
{
try
{
string requestUrl = application.Context.Request.Path;
string[] tmpUrl = requestUrl.Split('/');
string shopname = tmpUrl[tmpUrl.Length-1];

if (shopname.Split('.').Length == 1)// && (tmpUrl[tmpUrl.Length - 2] == "bbgang.com" || tmpUrl[tmpUrl.Length - 2]=="www.bbgang.com")
{
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["SQLConn"].ToString());
conn.Open();
SqlCommand cmd = new SqlCommand("select Id,Account from shop where [Account]='" + shopname + "'", conn);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
string shopid = sdr[0].ToString();
application.Context.RewritePath("~/shoppages/shop.aspx?sid=" + shopid);
}

conn.Close();
}
}
catch (Exception ex) {
//
}
}

#endregion
}
HarrisonCao 2009-12-30
  • 打赏
  • 举报
回复
http://bbgang.com/shoppages/shop.aspx?sid=8411fee9-6043-420b-b05e-30a656293c3d
jshi123 2009-12-30
  • 打赏
  • 举报
回复
http://bbgang.com/shoppages/shop.aspx?sid=1
直接打开这个页面,也是出错,说明你的网站还没有部署好,先把这个页面搞定了再说吧
三碗猪脚 2009-12-29
  • 打赏
  • 举报
回复
类文件 以前收藏的一段代码,只要配置web.config文件,但如何重定位需要自己写,比较灵活


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
/// <summary>
///HttpModule 的摘要说明
/// </summary>
public class UrlReWriter: IHttpModule
{
public UrlReWriter()
{
//
//TODO: 在此处添加构造函数逻辑
//
}

public void Dispose()
{
//这里写Dispose代码
}

public void Init(HttpApplication context)
{
//context.BeginRequest是开始处理HTTP管线请求时发生的事件
context.BeginRequest += new EventHandler(context_BeginRequest);
//context.Error是当处理过程中发生异常时产生的事件
context.Error += new EventHandler(context_Error);

}

void context_Error(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
context.Response.Write("<html>");
context.Response.Write("<head><title>出错了~~</title>");
context.Response.Write("</head>");
context.Response.Write("<body style=\"font-size:14px;align:center;BORDER:#bfceff 1px solid; height:18;FILTER:progid:DXImageTransform.Microsoft.Gradient(gradientType=0,startColorStr='#ddecfb',endColorStr='#ffffff'); width:180;\">");
context.Response.Write("<div name=\"errormessage\" ><p>");
context.Response.Write("对不起出错了:<br /><br />");
context.Response.Write(HttpUtility.HtmlEncode(context.Server.GetLastError().ToString()));
context.Response.Write("</p></div>");
context.Response.Write("</body></html>");
context.Response.End();
}

void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
HttpResponse response = context.Response;
string path = context.Request.Path;
string file = System.IO.Path.GetFileName(path);
//重写后的URL地址
Regex regex = new Regex("Down-(\\d+).aspx", RegexOptions.Compiled);
Match match = regex.Match(file);
//如果满足URL地址重写的条件
if (match.Success)
{
string userId = match.Groups[1].Value;
string rewritePath = "Down.aspx?aid=" + userId;
//将其按照UserInfo.aspx?UserId=123这样的形式重写,确保能正常执行
context.RewritePath(rewritePath);
}
}

}




<httpModules>
<add name="UrlReWriter" type="UrlReWriter"/><!--接管请求,地址重写-->
</httpModules>
shelless 2009-12-29
  • 打赏
  • 举报
回复
服务器上还要设置。
zjx311989 2009-12-29
  • 打赏
  • 举报
回复
呵呵 飘过..
wuyq11 2009-12-29
  • 打赏
  • 举报
回复
贴出代码
HarrisonCao 2009-12-29
  • 打赏
  • 举报
回复
具体怎么配置 在哪里配置 可否详细说明?
wuyq11 2009-12-29
  • 打赏
  • 举报
回复
是如何配置的,URLREWRITER配置问题

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧