Limit a method to a certain HTTP verb.

public class TodoController : Controller
    {

        /// <summary>
        /// Will only get called by POST verb.
        /// </summary>
        /// <returns></returns>
        public IActionResult ItemPost()
        {
            // I'm using autofac as IoC container
            ITodoManager todoManager = Program.Container.Resolve<ITodoManager>();
            int id = int.Parse(Parameters["id"]);
            TodoItem item = todoManager.Get(id);
            if (item == null)
                throw new NotFoundException("Failed to find TODO item #" + id);


            // Parameters are query string and form items combined.
            item.Title = Parameters["Title"];


            todoManager.Update(item);

            return Redirect("index");
        }

        public IViewData ItemGet()
        {
            int id = int.Parse(Parameters["id"]);
            ViewData["Item"] = Program.Container.Resolve<ITodoManager>().Get(id);
            return Render();
        }

        /// <summary>
        /// Creates a new controller that is a clone of the current one.
        /// </summary>
        /// <returns>
        /// A new controller.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override object Clone()
        {
            return new TodoController();
        }
    }

Both ItemGet and ItemPost got the uri htttp://yourserver/todo/item, the diffrence is that ItemGet is called when the GET http verb is used, and ItemPost is called when the POST http verb is used.

Last edited Feb 10, 2010 at 11:03 AM by jgauffin, version 1

Comments

No comments yet.