Copied to clipboard

Flag this post as spam?

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


  • webmonger 130 posts 285 karma points
    Jan 21, 2016 @ 15:57
    webmonger
    0

    New routes in UmbracoApiController with PluginController

    I'm creating a new web API controller for some front end work I'm doing and I'd like to add a new route so I'm not passing querystring values but I'm not sure how to do this.

    [PluginController("MyPlugin")]
    public class MyControllerController : UmbracoApiController
    {
        public HttpResponseMessage Get(int nodeId, string myString)
        {
        // Something here
        }
    }
    

    I would like the route umbraco/MyPlugin/MyController/get/12/stringValue rather than /umbraco/MyPlugin/MyController/get?noideid=12&myString=stringValue

    Thanks Jon

  • Ian 178 posts 752 karma points
    Jan 21, 2016 @ 23:01
    Ian
    0

    Hi there see this https://our.umbraco.org/forum/developers/extending-umbraco/46086-UmbracoApiController-No-HTTP-resource-was-found-that-matches-the-request-URI

    My understanding was that if your controller accepted only one simple type umbraco would read the value from the url without the use of query strings this is not your use case so you could try register your own route with the url template which satisfies your need

  • webmonger 130 posts 285 karma points
    Jan 22, 2016 @ 09:44
    webmonger
    0

    Hi Ian

    Thanks for the reply, I seem to be able to get the route registered however I'm not sure how to register the route including the information passed in by PluginController.

    I know I could create my own attribute that did the same thing but I figured if there was something already there why re-invent the wheel.

    I guess what I'm looking for is the value that goes in the { } for the route when registering it. I browsed through all the routes I can see in the list provided by RouteCollection however in the 60+ routes I saw I could not see the one that registers something for PluginController.

    Anyone have any ideas?

  • Ian 178 posts 752 karma points
    Jan 22, 2016 @ 11:19
    Ian
    0

    I haven't tested it but if you look at the last post in the linked thread could you not do something like this, specifying each of the parameters you need in the url. Without doing that I'm pretty sure that by default the route table will only contain routes that umbraco automatically created, there won't be a pattern in there which is able to extract the values you need

    public class StartupEventHandlers : ApplicationEventHandler
    {
        public StartupEventHandlers()
        {
            UmbracoApplicationBase.ApplicationStarting += UmbracoApplicationStarting;
        }
    
        static void UmbracoApplicationStarting(object sender, System.EventArgs e)
        {
            RouteTable.Routes.MapHttpRoute(
                name: "MyPluginApi",
                routeTemplate: "umbraco/MyPlugin/{controller}/{noideid}/{myString}",
                defaults: new
                {
                    noideid = RouteParameter.Optional,
                    myString = RouteParameter.Optional
                }
        }
    }
    

    Note the caution in this link though http://stackoverflow.com/questions/27692606/multiple-optional-parameters-routing , you may encounter difficulties if another route is registered that tries to handle the url before yours.

  • webmonger 130 posts 285 karma points
    Jan 22, 2016 @ 12:58
    webmonger
    0

    Thanks Ian

    Yeah I have that working and it's fine I just wondered if there was a way to do it with the built in solution for Plugins as it'll conver me in the case you suggest.

    Whilst unlikley it's possible to get a route clash in this situation, I would still prefer to just add a route to the collection with the correct paramiters to include the plugin information.

    I guess I'll have to do some more source code digging to work out where it's at.

    Cheers for the help

Please Sign in or register to post replies

Write your reply to:

Draft