URL映射是ASP.NET 2.0中提供的新特性。URL映射技术帮助我们将一个特定URL映射为另一个URL。为了帮助理解,我们假设你在站点有一个叫Homepage.aspx的页面来访问主页,所有的用户也都用这个页面来访问你的主页。但由于某些原因,你要将主页改为OriginalHome.aspx。此时使用URL映射让你可以映射到新页面,而不必通知用户。
如果我们设置了URL映射,那么任何用户在URL栏输入Homepage.aspx时,调用的都是OriginalHome.aspx。
深入概念:
让我们看看如何实现它。
这可以在configuration部分实现。
语法:
<urlMappings enabled="[true|false]">
<add url="String"
mappedUrl="String"/>
</urlMappings>
如果你想使用URL映射,你必须将enabled属性设置为true。每个add元素都含有一个原URL和映射URL。对,概念很简单!如果我们为上述场景配置了URL映射,config文件中的元素显示如下:
<urlMappings enabled="true">
<add url="~/ Homepage.aspx" mappedUrl="~/ originalHome.aspx"/>
</urlMappings>
一旦我们进行了修改或在工程的web.config文件中增加了如上的元素,任何用户试图访问Homepage.aspx时,由于URL映射,都将调用OriginalHome.aspx页面。有趣的是在URL栏中仍然仅显示Homepage.aspx。所以尽管想到内部调用/执行了OriginalHome.aspx,用户在URL栏仍见到Hopepage.aspx。
一些优点:
1.若你的客户标记了到一个页面的链接,但你又得删除该页并在换上其他页面,那么使用URL映射你可以解决这个商业问题而不用让客户知道这个页面变化。
2.若有一个大而复杂的URL,但不想把它给用户,那么你可以告知简单的URL,而自己将简单的URL映射到原先的URL。
3. 用这个方法可以方便处理菜单控件。最好的例子就是asp.net站点。
4.这里也牵涉到安全(用户无法在URL栏看到真实的页面名,这也是一种加密!)。
希望你喜欢这个新特性。
是的,我知道,现在你的问题是,既然现在ASP.NET 2.0还没有面市,怎么利用这个新特性呢。这里有个变通方法。Stev在他的blog里进行了简要的解释。
你可以在http://weblogs.asp.net/ssmith/archive/2003/11/06/36191.aspx看到。他的解释非常简单,根据你的实际案例,你可以为它增加功能。
现在你可以使用这个技巧,而等ASP.NET 2.0面市,你就可以利用新特性了。
引用内容
如果我们设置了URL映射,那么任何用户在URL栏输入Homepage.aspx时,调用的都是OriginalHome.aspx。
深入概念:
让我们看看如何实现它。
这可以在configuration部分实现。
语法:
<urlMappings enabled="[true|false]">
<add url="String"
mappedUrl="String"/>
</urlMappings>
如果你想使用URL映射,你必须将enabled属性设置为true。每个add元素都含有一个原URL和映射URL。对,概念很简单!如果我们为上述场景配置了URL映射,config文件中的元素显示如下:
<urlMappings enabled="true">
<add url="~/ Homepage.aspx" mappedUrl="~/ originalHome.aspx"/>
</urlMappings>
一旦我们进行了修改或在工程的web.config文件中增加了如上的元素,任何用户试图访问Homepage.aspx时,由于URL映射,都将调用OriginalHome.aspx页面。有趣的是在URL栏中仍然仅显示Homepage.aspx。所以尽管想到内部调用/执行了OriginalHome.aspx,用户在URL栏仍见到Hopepage.aspx。
一些优点:
1.若你的客户标记了到一个页面的链接,但你又得删除该页并在换上其他页面,那么使用URL映射你可以解决这个商业问题而不用让客户知道这个页面变化。
2.若有一个大而复杂的URL,但不想把它给用户,那么你可以告知简单的URL,而自己将简单的URL映射到原先的URL。
3. 用这个方法可以方便处理菜单控件。最好的例子就是asp.net站点。
4.这里也牵涉到安全(用户无法在URL栏看到真实的页面名,这也是一种加密!)。
希望你喜欢这个新特性。
是的,我知道,现在你的问题是,既然现在ASP.NET 2.0还没有面市,怎么利用这个新特性呢。这里有个变通方法。Stev在他的blog里进行了简要的解释。
你可以在http://weblogs.asp.net/ssmith/archive/2003/11/06/36191.aspx看到。他的解释非常简单,根据你的实际案例,你可以为它增加功能。
现在你可以使用这个技巧,而等ASP.NET 2.0面市,你就可以利用新特性了。
引用内容Regex To The Rescue For Shorter URLs
I've been redesigning AspAlliance.com off and on for the last several months, and I made a few more changes this morning. The big one that is noticeable to the general public is the URLs. Instead of having to link to articles via a viewer ASPX page and a series of querystring values, it is now sufficient to simply append the article ID to the end of the domain name (after a slash), like so:
http://aspalliance.com/1 (article ID 1, which is my Excel Reports in ASP article).
The nice thing about this is that it uses Context.RewritePath, so there is no Response.Redirect and the user never sees the actual URL of the page handling the request. The regex I'm using is here:
http://regexlib.com/REDetails.aspx?regexp_id=456
The actual code looks like this:
string originalUrl = Request.Url.ToString();
// Check for article shortcuts (e.g. http://aspalliance.com/1 )
string newUrl = AspAlliance.Web.Core.HttpRedirect.GetRedirect(originalUrl);
if(newUrl != originalUrl)
{
System.Uri myUri = new System.Uri(newUrl);
Context.RewritePath(myUri.PathAndQuery);
}
// GetRedirect:
System.Text.RegularExpressions.Regex regex =
new System.Text.RegularExpressions.Regex(@"\.com/(\d+)$",
(System.Text.RegularExpressions.RegexOptions.Compiled |
System.Text.RegularExpressions.RegexOptions.IgnoreCase));
System.Text.RegularExpressions.MatchCollection matches = regex.Matches(badRequest);
if(matches.Count > 0)
{
string id = matches[0].Value.Replace(".com/", "");
int aId;
try
{
aId = Int32.Parse(id);
return "http://aspalliance.com/articleviewer.aspx?aId=" + id;
}
catch
{}
}
return badRequest;
http://workgroup.cn/CS/blogs/aspnet2control/archive/2006/07/07/1411.aspx
I've been redesigning AspAlliance.com off and on for the last several months, and I made a few more changes this morning. The big one that is noticeable to the general public is the URLs. Instead of having to link to articles via a viewer ASPX page and a series of querystring values, it is now sufficient to simply append the article ID to the end of the domain name (after a slash), like so:
http://aspalliance.com/1 (article ID 1, which is my Excel Reports in ASP article).
The nice thing about this is that it uses Context.RewritePath, so there is no Response.Redirect and the user never sees the actual URL of the page handling the request. The regex I'm using is here:
http://regexlib.com/REDetails.aspx?regexp_id=456
The actual code looks like this:
string originalUrl = Request.Url.ToString();
// Check for article shortcuts (e.g. http://aspalliance.com/1 )
string newUrl = AspAlliance.Web.Core.HttpRedirect.GetRedirect(originalUrl);
if(newUrl != originalUrl)
{
System.Uri myUri = new System.Uri(newUrl);
Context.RewritePath(myUri.PathAndQuery);
}
// GetRedirect:
System.Text.RegularExpressions.Regex regex =
new System.Text.RegularExpressions.Regex(@"\.com/(\d+)$",
(System.Text.RegularExpressions.RegexOptions.Compiled |
System.Text.RegularExpressions.RegexOptions.IgnoreCase));
System.Text.RegularExpressions.MatchCollection matches = regex.Matches(badRequest);
if(matches.Count > 0)
{
string id = matches[0].Value.Replace(".com/", "");
int aId;
try
{
aId = Int32.Parse(id);
return "http://aspalliance.com/articleviewer.aspx?aId=" + id;
}
catch
{}
}
return badRequest;
http://workgroup.cn/CS/blogs/aspnet2control/archive/2006/07/07/1411.aspx
浙公网安备 33010602011771号