编程开发 > ASP > 文章内容

利用ASP.NET3.5的路由功能创建搜索引擎友好的URL(3)

2010-10-20编辑:dan

3、创建自定义路由处理程序,在网站工程中添加新项“类”,命名为ArticleRouteHandler.cs,类的网站内容如下:

 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Routing;
using System.Web.Compilation;
/// <summary>
///ArticleRouteHandler 的摘要说明
/// </summary>
public class ArticleRouteHandler : IRouteHandler
{
public ArticleRouteHandler()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
string ArticleGuid = requestContext.RouteData.Values["ArticleGuid"] as string;
HttpContext context = HttpContext.Current;
context.Items.Add("ArticleGuid", ArticleGuid);
string action = (requestContext.RouteData.Values["Action"] as string).ToLower();
if (action.Equals("read"))
return BuildManager.CreateInstanceFromVirtualPath("~/ArticleShow.aspx", typeof(Page)) as Page;
else if (action.Equals("new"))
return BuildManager.CreateInstanceFromVirtualPath("~/ArticleAdd.aspx", typeof(Page)) as Page;
else if (action.Equals("edit"))
return BuildManager.CreateInstanceFromVirtualPath("~/ArticleEdit.aspx", typeof(Page)) as Page;
else if (action.Equals("comment"))
return BuildManager.CreateInstanceFromVirtualPath("~/ArticleComment.aspx", typeof(Page)) as Page;
else if (action.Equals("print"))
return BuildManager.CreateInstanceFromVirtualPath("~/ArticlePrint.aspx", typeof(Page)) as Page;
else
return BuildManager.CreateInstanceFromVirtualPath("~/ArticleList.aspx", typeof(Page)) as Page;
}
}

 

注意:

 


using System.Web.UI;
using System.Web.Routing;
using System.Web.Compilation;

 

这3行代码是新加的。

利用ASP.NET3.5的路由功能创建搜索引擎友好的URL(2)

热点推荐

登录注册
触屏版电脑版网站地图