打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Creating Vanity URLs

Creating Vanity URLs

One interesting feature of the ASP.NET job site is its use of vanity URLs. Each user who registers at the job site is automatically given a personalized home page. For example, if you register with the username marksmith, you can access your home page by going to /aspnetjobs/marksmith.aspx (see Figure 30.4).

Figure 30.4. A vanity URL.

The vanity URLs are implemented with two files: the Global.asax and UserPage.aspx files. The Global.asax file checks the path of each request made to the job site. When appropriate, the Global.asax file rewrites the path of the request to the UserPage.aspx page. The complete code for the Global.asax file is contained in Listing 30.7.

Listing 30.7 Global.asax
<%@ Import Namespace="System.IO" %>

The C# version of this code can be found on the CD-ROM.

The Global.asax file in Listing 30.7 handles the BeginRequest event that is raised whenever someone requests a page from the job site. When appropriate, the RewritePath method is used to redirect a page request to the UserPage.aspx page.

First, the Application_BeginRequest subroutine checks whether the request is for the Default.aspx page, the Userpage.aspx page, or a page below the /site/ subdirectory. These requests are ignored. You only want to redirect a user when the user makes a request with a vanity URL. You must be careful not to redirect users when they are requesting actual pages at the job site.

NOTE

You also can implement vanity URLs by creating a custom HTTP module. An HTTP module is a class that is executed whenever a request is made for an ASP.NET page. To learn more about creating HTTP modules, see Chapter 15, "Creating ASP.NET Applications."


If the user is not requesting an actual page at the Web site, the RewritePath method is used to redirect the user to the UserPage.aspx page. For example, if the user requests

/aspnetjobs/marksmith.aspx

the Global.asax file automatically rewrites the request like this:

/aspnetjobs/userpage.aspx?pageowner=marksmith

The value of the pageowner query string variable is captured within the UserPage.aspx page. The pageowner variable is used within the UserPage.aspx page to look up and display the information for the appropriate user from the database.

When using the Global.asax file in Listing 30.7, you must be cautious about the physical layout of the files in your Web site. All the actual files for the job site are located beneath the /site/ subdirectory. If you add a new page to the root directory, the page could never be accessed because any requests for the page would be interpreted as a request for a vanity URL. If you need to add new files to the ASP.NET job site, add the new files beneath the /site/ subdirectory.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
使用Global.asax实现ASP.Net的URL重写
一分钟学个词|Vanity
居然可以这样?| 利用CDN的“小功能”简化Web运维
C# MVC网站自动由HTTP转为HTTPS
ASP.NET MVC架构与实战系列之二:理解MVC路由配置
HttpApplication的认识与加深理解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服