Copied to clipboard

Flag this post as spam?

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


  • Rasmus Fjord 675 posts 1566 karma points c-trib
    Dec 04, 2014 @ 10:23
    Rasmus Fjord
    0

    Using WebApi as REST service

    Hey :) 

    Right now im using the brilliant webapi and has a url like this 

    /Umbraco/Api/Comment/Create

    But what i really would like would a way to remove the "create" part and just use [HttPost] and [HttpGet]

    So the url would be just like this

    /Umbraco/Api/Comment

     

    Is this possible ?

  • Sören Deger 733 posts 2844 karma points c-trib
    Dec 04, 2014 @ 11:13
    Sören Deger
    1

    Hi Rasmus,

    I think you can do this. I have a similiar thing at this post:

    http://our.umbraco.org/forum/umbraco-7/developing-umbraco-7-packages/59395-Custom-route-for-webApiController

    You can customize your route like this:

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
    RouteTable.Routes.MapHttpRoute("YourCustomRouteName", "Umbraco/Api/Comment", new { controller = "YourControllerName", action = "YourActionName" });
    }
    

    You can give one of your action method the attribute [HttpGet] and your second action with the same name the attribute [HttpPost] in your ApiController. I have the example with attributes not tested yet, but at least in theory it should work as this.

    Hope this helps.


    Sören 

  • Rasmus Fjord 675 posts 1566 karma points c-trib
    Dec 04, 2014 @ 12:05
    Rasmus Fjord
    0

    Hey Soren :) 

    Thx for a possible solution.

    So now i might be asking a "funny" question, but where shall i put this ? Im gussing its in a global asax file right ? 

    But in Umbraco there is already a global.asax file, adding another wont this frack it up ? any pointers would be awesome-tastic!

  • Sören Deger 733 posts 2844 karma points c-trib
    Dec 04, 2014 @ 12:16
    Sören Deger
    100

    Hi Rasmus,

    you can put the first code in an eventhandler. An eventhandler in umbraco is a simple class (cs file). See the link above or here (http://our.umbraco.org/documentation/Reference/Events-v6/).

    The attributes [HttpGet] or [HttpPost] you must put over your action controller in your own webApiController (see here: http://our.umbraco.org/documentation/Reference/WebApi/)

     

    Cheers

    Sören

  • Rasmus Fjord 675 posts 1566 karma points c-trib
    Dec 04, 2014 @ 12:23
    Rasmus Fjord
    0

    Awesome thx ill spin it and get back 

     

  • Rasmus Fjord 675 posts 1566 karma points c-trib
    Dec 04, 2014 @ 15:42
    Rasmus Fjord
    0

    Hey Soren :) 

    Ive tried but i think im missing something somewhere.

    Ive created a simple class looking like this, added it to the App_Code :

      public class EventHandler : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                RouteTable.Routes.MapHttpRoute("Comments", "Umbraco/Api/Comment", new { controller = "CommentController", action = "Comment" });
            }
        }

    So that seems okay i guess, then i have a Controller looking like this :

     public class CommentController : UmbracoApiController
        {
            [HttpPost]
            public HttpResponseMessage Comment(CommentModel model)
            {
                var service = new ContentService();
                var currentmember = Members.GetCurrentMember();
                var project = service.GetById(Convert.ToInt32(model.QuestionId));
    
                var comment = service.CreateContent(
                DateTime.Now.ToUnixTime().ToString(), 
                project.Id,  
                "kommentar", 
                0);
    
                comment.SetValue("tekst", model.Comment);
                comment.SetValue("bruger", currentmember.Id);
    
                service.SaveAndPublishWithStatus(comment);
    
                return new HttpResponseMessage(HttpStatusCode.OK);
            }
    }

    When i "post" i get a 404, can you see me missing anything

  • Rasmus Fjord 675 posts 1566 karma points c-trib
    Dec 05, 2014 @ 12:13
    Rasmus Fjord
    1

    I got it working :) like this :

     protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
    
                RouteTable.Routes.MapHttpRoute("CommentApi", "Api/Questions/{QuestionId}/Comments", new { controller = "Comment", action = "Index", QuestionId = UrlParameter.Optional });
            }

    I think the umbraco/api is not available to add to.

  • Sören Deger 733 posts 2844 karma points c-trib
    Dec 08, 2014 @ 10:27
    Sören Deger
    0

    Hi Rasmus,

    sorry for the late response. I had a few days off. Nice to hear that you can solved it. I have a very similar project for the beginning of next year. I will tell you my experience with this if I have implemented this. Maybe I find another way to solve this. I'll keep you up to date.


    Sören

  • Rasmus Fjord 675 posts 1566 karma points c-trib
    Dec 08, 2014 @ 13:22
    Rasmus Fjord
    0

    Sure thing Soren :)

    The solution above works like a charm, only thing to be aware of is i think "/Umbraco/Api" is reserved and gussing thats why it didnt work in the first place.

Please Sign in or register to post replies

Write your reply to:

Draft