public class CustomRouteHandler : IRouteHandler { public IHttpHandler GetHttpHandler(RequestContext requestContext) { // Retrieve the controller and action from the route data string controllerName = requestContext.RouteData.Values["controller"].ToString(); string actionName = requestContext.RouteData.Values["action"].ToString(); // Create a new instance of the controller and execute the action var controller = (ControllerBase)Activator.CreateInstance(Type.GetType(controllerName + "Controller")); return controller.Execute(new RequestContextWrapper(requestContext), actionName); } }
public class MyRouteHandler : IRouteHandler { public IHttpHandler GetHttpHandler(RequestContext requestContext) { return new MyHttpHandler(); } } public class MyHttpHandler : IHttpHandler { public bool IsReusable => true; public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Write("Hello, World!"); } }In this example, a custom route handler is created that returns a custom HTTP handler `MyHttpHandler` that outputs "Hello, World!" as plain text. This demonstrates how a custom route handler can be used to create custom HTTP handlers for specific routes. Package/library: System.Web.Routing.