Telerik blogs

If you really, really like the new and shiny extensions, you can use them in your WebForms application, too!

Here are the needed steps:

  1. Add references to the following assemblies:

    • System.Web.Abstractions
    • System.Web.Routing
    • System.Web.Mvc
    • Telerik.Web.Mvc

    The first three are usually in the GAC.

  2. Copy the Content and Scripts folders from the extensions’ distribution into your project.

  3. The pages which will contain the MVC components will need to inherit System.Web.Mvc.ViewPage. Also, you should mock an MVC environment, like this:

    protected class ControllerMock : Controller
    {
        public ControllerMock(NameValueCollection queryString)
        {
            foreach (string param in queryString.Keys)
                ValueProvider.Add(
                    new KeyValuePair<string, ValueProviderResult>
                        (param, new ValueProviderResult(
                            queryString.Get(param), null,
                            CultureInfo.CurrentCulture)));
        }
    }
    
    protected void Page_Init(object sender, EventArgs e)
    {
        var contextWrapper = new HttpContextWrapper(HttpContext.Current);
    
        var controllerMock = new ControllerMock(Request.QueryString);
    
        var routeData = new RouteData();
    
        this.ViewContext = new ViewContext()
        {
            Controller = controllerMock,
            HttpContext = contextWrapper,
            RequestContext = new RequestContext(contextWrapper, routeData)
        };
    
        this.InitHelpers();
    }
  4. Add a Global.asax file to register the MVC routes - at least one route is needed for proper URL generation:
    static void RegisterRoutes(System.Web.Routing.RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "FirstLook", id = "" });
    }
    
    void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes(System.Web.Routing.RouteTable.Routes);
    }
  5. Use the extensions!

And here is a sample project to get it up-and-running in no time:

Guess which Telerik project needs this ;-)


About the Author

Alexander Gyoshev

 is Senior Front-end Developer in Kendo Team

Comments

Comments are disabled in preview mode.