看网上说嘴简单方法在GLOBLE里面写string path = Request.Url.ToString();
if (path.EndsWith("/test/WebForm3.aspx"))
{
Context.RewritePath("/test/WebForm3-11.aspx?");
}
这样写了以后,跑程序出现无法找到资源的错误吗.首页就是WebForm3.aspx,应该怎么写的,知道的来指点下吧
if (path.EndsWith("/test/WebForm3.aspx"))
{
Context.RewritePath("/test/WebForm3-11.aspx?");
}
这样写了以后,跑程序出现无法找到资源的错误吗.首页就是WebForm3.aspx,应该怎么写的,知道的来指点下吧
这样写:
string path = Request.Url.ToString();
if (path.EndsWith("../WebForm3.aspx"))
{
Context.RewritePath("../WebForm3-11.aspx?");
}
if (path.EndsWith("WebForm3.aspx"))
{
Context.RewritePath("../WebForm3-11.aspx?");
}
这样行吧
你写过没有呢?感觉这样写不是URL重写,报错了,他会去找WebForm3-11这个页面.但这个页面是不存在的
说明: HTTP 404。您正在查找的资源(或者它的一个依赖项)可能已被移除,或其名称已更改,或暂时不可用。请检查以下 URL 并确保其拼写正确。
Response.Redirect("WebForm3-11.aspx",true);
我看你的代码觉得很费解
你的意思是用户看到的(或是输入的)url是/test/WebForm3.aspx格式的
你要转到你网站项目下的哪个文件啊??难道是WebForm3-11.aspx???
而你又说你根本没有这个文件 这样你怎么转呢???我理解的简单的例子
string path = Request.Url.ToString();
if (path.EndsWith("/test/WebForm3.aspx"))//这是用户看到的(输入的)
{
Context.RewritePath("/test/WebForm.aspx?id=3");//这是你服务器处理的 要确定WebForm.aspx是存在的
}不对之处请指教
WebForm3-11.aspx 这个页面没有的话那你想要往哪里连???
http://www.store.com/products.aspx?category=DVDs
http://www.store.com/products.aspx?category=CDs 但我们不想使用查询字符串来呈示每个类别,我们想修改应用,让每个产品类别对搜索引擎来说看上去象是一个独特的URL,并且在实际的URL中嵌入关键词(而不是通过查询字符串参数)。我们将在这个博客帖子剩下来的篇幅里,讨论一下达成这个目的我们可以采取的4种不同方法。 方法一:使用Request.PathInfo 参数而不是查询字符串我将示范的第一个方法根本不使用URL重写,而是使用ASP.NET中不太为人所知的一个特性,Request的PathInfo属性。为帮助解释这个属性的有用之处,考虑一下我们电子商店下面这些URL的情形: http://www.store.com/products.aspx/Books
http://www.store.com/products.aspx/DVDs
http://www.store.com/products.aspx/CDs
你会在上面这些URL中注意到的一个东西是,他们不再含有查询字符串值,取而代之的是,类别参数的值是附加到URL上的,是以Products.aspx网页处理器名称之后的/参数 值的方式出现的。然后,一个自动化的搜索引擎爬虫(search engine crawler)会把这些URL解释成三个不同的URL,而不是一个URL带有三个不同的输入值 (搜索引擎是不理会文件扩展名的,只把它当作URL中的另一个字符而已)。
if (path.EndsWith("/test/WebForm3-11.aspx"))
{
Context.RewritePath("/test/WebForm3.aspx?id=11");
}我看你是不是写反了,是不是想把WebForm3-11.aspx转到WebForm3.aspx?
RewritePath后面的才是"真实的"url 就是为了不给用户看到!!
是在什么事件所处何生命周期里进行更改的,
这是关键啊.
那这么简单的说一下吧.我现在打开一个页面是http://localhost/test/WebForm3.aspx?id=11如果我不做任何修改,IE地址栏里面将显示该地址,现在我想通过我上面的方法,让IE地址栏里面显示的是
http://localhost/test/WebForm3-11.aspx?
这样的形式,那怎么做.用我上面的方法可以实现吗?
2. 一般自己写Url重写是使用HttpHandle,如果你用的是VS2003,没有asp.net 2.0好实现
if(path.Contains("WebForm3-"))
{
int heng = path.LastIndexOf("-");//横
int dot = path.LastIndexOf(".");
if (dot - heng != 1)
{
string id = path.Substring(heng + 1, dot - heng - 1);
Context.RewritePath("~/WebForm3.aspx?id=" + num);
}
} 晕死 写的太笨了 谁来个正则解决下 最好连WebForm1 WebForm2也能考虑的
<add url="~/test/WebForm3-11.aspx" mappedUrl="~/test/WebForm3.aspx"/>
</urlMappings>
上述Request.PathInfo技术的替换方法是,利用ASP.NET提供的HttpContext.RewritePath方法。这个方法允许开发人员动态地重写收到的URL的处理路径,然后让ASP.NET使用刚重写过后的路径来继续执行请求。 譬如,我们可以选择向大众呈示下列URL:
http://www.store.com/products/Books.aspx
http://www.store.com/products/DVDs.aspx
http://www.store.com/products/CDs.aspx 在外界看来,网站上有三个单独的网页(对搜索爬虫而言,这看上去很棒)。通过使用 HttpContext的RewritePath方法,我们可以在这些请求刚进入服务器时,动态地把收到的URL重写成单个Products.aspx网页接受一个查询字符串的类别名称或者PathInfo参数。譬如,我们可以使用Global.asax中的Application_BeginRequest事件,来这么做: void Application_BeginRequest(object sender, EventArgs e) { string fullOrigionalpath = Request.Url.ToString();
if (fullOrigionalpath.Contains("/Products/Books.aspx")) {
Context.RewritePath("/Products.aspx?Category=Books");
}
else if (fullOrigionalpath.Contains("/Products/DVDs.aspx")) {
Context.RewritePath("/Products.aspx?Category=DVDs");
}
}