1

A bit of sleep deprived experimentation here..

I've been playing around with Backbone.js recently and are wondering if there's a way to combine the client-side routing with the server-side routing of umbraco. They way I think about it is instead of the typical RenderMvcController ActionResult being returned for a template name in the route table, the controller would redirect to UmbracoApiController to return json - in effect handing off the task of routing to templates to the backbone client.

This is where I'm stuck, redirecting to the UmbracoApiController - hoping someone can see what I've missed.

Cheers!

Edit: Forgot the error message I'm getting.

No route in the route table matches the supplied values.

public class HomeController : Umbraco.Web.Mvc.RenderMvcController
{
    [HttpGet]
    public ActionResult Index()
    {
        return RedirectToAction("GetAllProducts", "ProductsApi");
    }
}
public class ProductsApiController : Umbraco.Web.WebApi.UmbracoApiController
{
    public IEnumerable<string> GetAllProducts()
    {
        return new[] { "Table", "Chair", "Desk", "Computer", "Beer fridge" };
    }
}


public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "Umbraco/Api/{controller}/{action}/",
            defaults: new { id = RouteParameter.Optional }
        );

        var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
        config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
    }
}

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }
}
MikeW
  • 4,749
  • 9
  • 42
  • 83

1 Answers1

0

I got a similar thing working by extending PluginController rather than UmbracoApiController, following the approach here.

Community
  • 1
  • 1
nullPainter
  • 2,676
  • 3
  • 22
  • 42