我正在使用Asp.net 3.5,我想在项目中使用URL路由 在global.asax中,我给出了这样的代码
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
void RegisterRoutes(RouteCollection routes)
{
routes.Clear();
routes.Add(
"Blogcat",
new Route("{Category}", new PostRouteHandler())
);
}
protected void Session_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
PostRouteHandler.cs文件,我给出这样的代码
public class Helpers
{
public static string FormatProductUrl(string Category)
{
return RouteTable.Routes.GetVirtualPath(null, "Blogcat", new RouteValueDictionary { { "Category", Category } }).VirtualPath;
}
public static IHttpHandler GetNotFoundHttpHandler()
{
return BuildManager.CreateInstanceFromVirtualPath("~/NotFound.aspx", typeof(Page)) as Page;
}
}
public class PostRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
string Category = requestContext.RouteData.Values["Category"] as string;
if (string.IsNullOrEmpty(Category))
return Helpers.GetNotFoundHttpHandler();
else
{
if (Category == null)
return Helpers.GetNotFoundHttpHandler();
else
{
HttpContext.Current.Server.UrlEncode(Category);
return BuildManager.CreateInstanceFromVirtualPath("~/Default.aspx", typeof(Page)) as Page;
}
}
}
当我要提供Url http://localhost:63645/BlogCat/123时 它显示未找到URL。请帮助我,我的代码有什么问题。