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.
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/).
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
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.
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.
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 ?
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:
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
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!
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
Awesome thx ill spin it and get back
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 :
So that seems okay i guess, then i have a Controller looking like this :
When i "post" i get a 404, can you see me missing anything
I got it working :) like this :
I think the umbraco/api is not available to add to.
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
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.
is working on a reply...