欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MVC 5限制所有HTTP请求必须是POST方式

程序员文章站 2023-11-09 13:11:58
今天有位同事,提出了这样一个问题,他想限制所有mvc接收到的http请求必须是post方式。 接下来在下面的内容中,将我想到的方式分享给大家,如果大家有其它的方式,请留言...

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

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

 一、httppostattribute特性

大家首先想到时的,mvc提供了httppostattribute特性,是用于限制http请求必须post方式来提交。 

public class homecontroller : controller
 { 
 [httppost]
 public actionresult index()
 {
  return view();
 }
 }

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

//
 // 摘要:
 // 表示一个特性,该特性用于限制操作方法,以便该方法仅处理 http post 请求。
 [attributeusage(attributetargets.method, allowmultiple = false, inherited = true)]
 public sealed class httppostattribute : actionmethodselectorattribute
 {

 }

二、使用httpmodule

asp.net管线中,可以通过 httpmodule 对 httpapplication 对象中的事件注册自己的事件处理程序,来控制所有的http请求。

public class httpmethodmodule : ihttpmodule
 {
 public void init(httpapplication context)
 {
  context.postmaprequesthandler += context_postmaprequesthandler;
 }

 private void context_postmaprequesthandler(object sender, eventargs e)
 {
  httpapplication httpapplication = (httpapplication) sender;
  httpcontext httpcontext = httpapplication.context;


  //判断当前是否使用的是 mvc 框架来处理请求,其它的请示不做控制。
  mvchandler mvchandler = httpcontext.handler as mvchandler;

  if (mvchandler != null && httpcontext.ispostmethod() == false) {
  throw new httpexception(404, "访问的资源不存在。");
  }
 }

 public void dispose()
 {

 }
 }

在web.config增加相关的配置。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <system.webserver>
 <modules>
 <add name="httpmethod" type="httppostwebapp.web.httpmethodmodule, httppostwebapp"/>
 </modules>
 </system.webserver>
</configuration>

经过测试,是可以达到我们的要求(关于测试结果不在做演示)。

三、mvc过滤器

在mvc中,可以通过全局的过滤器来控制请求。

public class httppostfilter : iauthorizationfilter
 {
 public void onauthorization(authorizationcontext filtercontext)
 {
  if (filtercontext.httpcontext.ispostmethod() == false) {

  //如果不是post请求,则返回404。
  filtercontext.result = new httpnotfoundresult();
  }
 }
 }

在程序启动时,注册为全局过滤器。

public class filterconfig
 {
 public static void registerglobalfilters(globalfiltercollection filters)
 {
  filters.add(new httppostfilter());
 }
 }

四、路由约束

在注册路由时,可以定义路由的约束。通过如下方式,可以将请求方式限制为post请求。

public class routeconfig
 {
 public static void registerroutes(routecollection routes)
 {
  routes.maproute(
  name: "default",
  url: "{controller}/{action}/{id}",
  defaults: new { controller = "home", action = "index", id = urlparameter.optional }
  //限制请求方式必须是post
  , constraints:new { httpmethod = new httpmethodconstraint("post")}
  );
 }
 }

五、重写controller方法

在mvc中,所有控制器默认继承于controller。

我们可以定义一个basecontroller的抽象类,重写onactionexecuting,其它的控制器都继承于basecontroller。

public abstract class basecontroller : controller
 {
 protected override void onactionexecuting(actionexecutingcontext filtercontext)
 {
  
  if (filtercontext.httpcontext.ispostmethod() == false) {
  //如果不是post请求,则返回404。
  filtercontext.result = new httpnotfoundresult();
  }
  else {
  base.onactionexecuting(filtercontext);
  }
 }
 }

这种方法,需要修改所有控制器的基类,不推荐。

当然如果你已经定义了自己的控制器基类,这种方式的工作量也是非常小的。

总结

上述五种方法中,二、三、四方法都非常简单,但是我比较推荐方法四,因为如果需求发生变化,维护工作量是最小的。

如果大家有其它的方式,请留言,谢谢!

demo下载:mvchttppostwebapp

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。