今天有位同事,提出了这样一个问题,他想限制所有MVC接收到的HTTP请求必须是POST方式。

  接下来在下面的内容中,将我想到的方式分享给大家,如果大家有其它的方式,请留言。 

 一、HttpPostAttribute特性

  大家首先想到时的,MVC提供了HttpPostAttribute特性,是用于限制HTTP请求必须POST方式来提交。  

1   public class HomeController : Controller
2   {        
3         [HttpPost]
4         public ActionResult Index()
5         {
6             return View();
7         }
8   }

  这个特性只能在Action方法上面做标记,需要我们在每一个Action方法上面做标记,做一个Coder,这种方式,我们肯定接收不了。 

1     //
2     // 摘要:
3     //     表示一个特性,该特性用于限制操作方法,以便该方法仅处理 HTTP POST 请求。
4     [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
5     public sealed class HttpPostAttribute : ActionMethodSelectorAttribute
6     {
7 
8     }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2022-02-13
  • 2021-08-14
  • 2021-08-08
  • 2022-12-23
猜你喜欢
  • 2022-02-15
  • 2021-11-26
  • 2021-11-23
  • 2022-01-01
相关资源
相似解决方案