Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nicholas Magnussen 9 posts 73 karma points
    Oct 08, 2014 @ 11:29
    Nicholas Magnussen
    0

    Problem with custom routing in Umbraco 7

    Hello,

    I'm having some trouble writing custom routing for an MVC application that I'm developing.

    I've created a class in the App_Start folder called Startup that looks like this:

    public class Startup : IApplicationEventHandler
    {
        public void OnApplicationStarted(UmbracoApplicationBase application, ApplicationContext context)
        {
        }
    
        public void OnApplicationStarting(UmbracoApplicationBase application, ApplicationContext context)
        {
            RouteTable.Routes.MapRoute("GetCart", "Umbraco/Api/Cart", new { controller = "Cart", action = "Index" });
        }
    
        public void OnApplicationInitialized(UmbracoApplicationBase application, ApplicationContext context)
        {
        }
    }
    

    When I then visit the url: "site.com/Umbraco/Api/Cart" I get a blank page. But if I visit the url: "site.com/Umbraco/Api/Cart/Index" that the custom route should map to, I'm displayed the content that I want.

    Am I doing something wrong here?

    My CartController looks like this:

    public class CartController : UmbracoApiController
    {
        [HttpGet]
        [ActionName("Index")]
        public string GetCart()
        {
            return "";
        }
    
        [HttpGet]
        [ActionName("Mini")]
        public string GetMiniCart()
        {
            return "";
        }
    
        [HttpPost]
        [ActionName("Products")]
        public string ProductsAdd()
        {
            return "";
        }
    
        [HttpPut]
        [ActionName("Products")]
        public string ProductsUpdate(int productId)
        {
            return "";
        }
    
        [HttpDelete]
        [ActionName("Products")]
        public string ProductsDelete(int productId)
        {
            return "";
        }
    }
    
  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Oct 08, 2014 @ 11:44
    Dave Woestenborghs
    0

    First of all. You don't need to setup routing for UmbracoApiControllers. This handled by the framework.

    You can find some more documentation here :

    http://our.umbraco.org/documentation/Reference/WebApi/

    http://our.umbraco.org/documentation/Reference/WebApi/routing

    Especially the last link will be useful for you.

    Dave

  • Nicholas Magnussen 9 posts 73 karma points
    Oct 08, 2014 @ 11:52
    Nicholas Magnussen
    0

    Well, what I'm trying to do is have my urls make sense.

    Problem is, I have two CartControllers actually, one is an ApiController and one is a SurfaceController.
    I do not want my link to the ApiController to look like this: site.com/Umbraco/Api/CartApi - That doesnt't make sense. Instead I want it to be: site.com/Umbraco/Api/Cart. That is why I'm doing the custom routing.

    Problem being that, if I remove my custom routing I get a 404 trying the access the url: site.com/Umbraco/Api/Cart.

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Oct 08, 2014 @ 12:01
    Dave Woestenborghs
    0

    Looking at your code your route (without custom routing) would be /umbraco/api/cart/index

    But if you don't want to add the index method the only ways is custom routing, but then you wouldn't need to inherit from UmbracoApiController, but the default API controller

    Dave

  • Nicholas Magnussen 9 posts 73 karma points
    Oct 08, 2014 @ 12:18
    Nicholas Magnussen
    0

    Sadly, inheritting from the default ApiController did not help anything :-\

    I still get a blank page when accessing site.com/Umbraco/Api/Cart but now I simply can't access site.com/Umbraco/Api/Cart/Index of course, because I removed the routing automatically done by Umbraco.

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Oct 08, 2014 @ 12:34
    Dave Woestenborghs
    0

    If you inherit from API controller you should route your request :

    publicclassStartup:IApplicationEventHandler
    {
       
    publicvoidOnApplicationStarted(UmbracoApplicationBase application,ApplicationContext context)
       
    {
       
    }

       
    publicvoidOnApplicationStarting(UmbracoApplicationBase application,ApplicationContext context)
       
    {
           
    RouteTable.Routes.MapRoute("GetCart","Umbraco/Api/Cart/{action}",new{ controller ="Cart", action ="Index"});
       
    }

       
    publicvoidOnApplicationInitialized(UmbracoApplicationBase application,ApplicationContext context)
       
    {
       
    }
    }

    Dave

  • Nicholas Magnussen 9 posts 73 karma points
    Oct 08, 2014 @ 14:33
    Nicholas Magnussen
    0

    No change at all. I'm still just getting a blank page.
    Weird thing is that I don't get a 404. And if I set breakpoints, I don't hit the method in my controller.

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Oct 08, 2014 @ 14:40
    Dave Woestenborghs
    0

    And if you make action an optional url parameter ?

    Can I ask why you want your API url's like this? Is it going to be available to 3rd parties ?

    Dave

  • Nicholas Magnussen 9 posts 73 karma points
    Oct 08, 2014 @ 14:47
    Nicholas Magnussen
    0

    If I set action to UrlParameter.Optional I get the following error:
    routedata must contain an item named 'action' with a non-empty string value

    And you can absolutely ask why.
    Yes, it's part of a webshop, so we would like to make the url as "pretty" as possible :-)

  • Nicholas Magnussen 9 posts 73 karma points
    Oct 09, 2014 @ 11:02
    Nicholas Magnussen
    0

    Problem solved.

    To map the route correctly you will have to use RouteTable.Routes.MapHttpRoute instead of RouteTable.Routes.MapRoute and it will work perfectly.

Please Sign in or register to post replies

Write your reply to:

Draft