【发布时间】:2011-10-14 00:33:57
【问题描述】:
我在带有路由站点的 ASP.net 4.0 中工作,它不在 MVC 架构中。 这里我遇到了一个大问题,即我无法通过路由调用任何处理程序文件。
我在 global.asax 页面中编写此代码
public static void RegisterRoutes(System.Web.Routing.RouteCollection routes)
{
routes.Add(new System.Web.Routing.Route("{language}/{*page}", new CustomRouteHandler()));
}
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(System.Web.Routing.RouteTable.Routes);
}
在 CustomRouteHandler 类中
public class CustomRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
string language = TemplateControlExtension.GetString(null, requestContext.RouteData.Values["language"]).ToLower();
string page = TemplateControlExtension.GetString(null, requestContext.RouteData.Values["page"]).ToLower();
if (string.IsNullOrEmpty(page))
{
HttpContext.Current.Response.Redirect("/" + language + "/default.aspx");
}
string VirtualPath = "~/" + page;
if (language != null)
{
TemplateControlExtension.Language = language;
}
return BuildManager.CreateInstanceFromVirtualPath(VirtualPath, typeof(Page)) as IHttpHandler;
}
}
当我在此站点中调用任何处理程序文件时,它会引发错误,即
Type 'Captcha' does not inherit from 'System.Web.UI.Page'.
我的问题是我们如何在这个站点中调用处理程序文件??
这个路由代码要什么修改??
【问题讨论】:
-
它是一个页面吗?似乎您的路线正在接受此请求并且没有返回正确的值
标签: asp.net routing httphandler handler