如何在asp.net 4.0路由中调用*.ashx处理程序
我在ASP.NET4.0中使用路由站点,它不在MVC体系结构中。 这里我遇到了一个大问题,即我不能通过路由调用任何处理程序文件 我在global.asax页面中编写此代码如何在asp.net 4.0路由中调用*.ashx处理程序,asp.net,routing,httphandler,handler,Asp.net,Routing,Httphandler,Handler,我在ASP.NET4.0中使用路由站点,它不在MVC体系结构中。 这里我遇到了一个大问题,即我不能通过路由调用任何处理程序文件 我在global.asax页面中编写此代码 public static void RegisterRoutes(System.Web.Routing.RouteCollection routes) { routes.Add(new System.Web.Routing.Route("{language}/{*page}", new CustomR
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'.
我的问题是我们如何在这个站点中调用处理程序文件
此路线代码需要什么修改???使用此代码
using System.Web;
using System.Web.Compilation;
using System.Web.Routing;
public class HttpHandlerRouteHandler<T> : IRouteHandler where T : IHttpHandler, new() {
public HttpHandlerRouteHandler() { }
public IHttpHandler GetHttpHandler(RequestContext requestContext) {
return new T();
}
}
public class HttpHandlerRouteHandler : IRouteHandler {
private string _VirtualPath;
public HttpHandlerRouteHandler(string virtualPath) {
this._VirtualPath = virtualPath;
}
public IHttpHandler GetHttpHandler(RequestContext requestContext) {
return (IHttpHandler) BuildManager.CreateInstanceFromVirtualPath(this._VirtualPath, typeof(IHttpHandler));
}
}
使用System.Web;
使用System.Web.compilement;
使用System.Web.Routing;
公共类HttpHandlerRouteHandler:IRouteHandler,其中T:IHttpHandler,new(){
公共HttpHandlerRouteHandler(){}
公共IHttpHandler GetHttpHandler(RequestContext RequestContext){
返回新的T();
}
}
公共类HttpHandlerRouteHandler:IRouteHandler{
私有字符串VirtualPath;
公共HttpHandlerRouteHandler(字符串虚拟路径){
这个。_VirtualPath=VirtualPath;
}
公共IHttpHandler GetHttpHandler(RequestContext RequestContext){
return(IHttpHandler)BuildManager.CreateInstanceFromVirtualPath(this.u VirtualPath,typeof(IHttpHandler));
}
}
这是catpcha页面吗?似乎您的路由正在接受此请求,并且没有返回正确的值