PostOnly Filter

Note: The PostOnly filter has now been deprecated and will be removed when the next MVC Preview is released. Use the AcceptPostAttribute instead.

The PostOnly filter checks that an action is being called via an HTTP POST. If not, it will throw an exception. For example:

namespace Demo.Controllers
{
	using MvcContrib.Filters;

	public class HomeController : Controller
	{
		public ActionResult Index()
		{
			return View("Index");
		}

		[PostOnly]
		public ActionResult Save()
		{
			return View("Edit");
		}
	}
}


In the above controller, the Index action can be accessed normally. However, if you try to visit mysite.com/Home/Save then the filter will throw an exception:

Action 'Save' can only be accessed using an HTTP Post.


If the filter was applied to the controller, then it would affect both the Index and the Save actions.

[PostOnly]
public class HomeController : Controller
... 

Last edited Sep 13, 2008 at 4:30 PM by JeremyS, version 3

Comments

No comments yet.