您好,欢迎访问一九零五行业门户网

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

今天有位同事,提出了这样一个问题,他想限制所有mvc接收到的http请求必须是post方式。
接下来在下面的内容中,将我想到的方式分享给大家,如果大家有其它的方式,请留言。 
 一、httppostattribute特性大家首先想到时的,mvc提供了httppostattribute特性,是用于限制http请求必须post方式来提交。  
1   public class homecontroller : controller2   {         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 : actionmethodselectorattribute6     {7 8     }
view code
 二、使用httpmoduleasp.net管线中,可以通过 httpmodule 对 httpapplication 对象中的事件注册自己的事件处理程序,来控制所有的http请求。
 1     public class httpmethodmodule : ihttpmodule 2     { 3         public void init(httpapplication context) 4         { 5             context.postmaprequesthandler += context_postmaprequesthandler; 6         } 7  8         private void context_postmaprequesthandler(object sender, eventargs e) 9         {10             httpapplication httpapplication = (httpapplication) sender;11             httpcontext httpcontext = httpapplication.context;12 13 14             //判断当前是否使用的是 mvc 框架来处理请求,其它的请示不做控制。15             mvchandler mvchandler = httpcontext.handler as mvchandler;16 17             if (mvchandler != null && httpcontext.ispostmethod() == false) {18                 throw new httpexception(404, 访问的资源不存在。);19             }20         }21 22         public void dispose()23         {24 25         }26     }
在web.config增加相关的配置。
1 <?xml version="1.0" encoding="utf-8"?>2 <configuration>3   <system.webserver>4     <modules>5       <add name="httpmethod" type="httppostwebapp.web.httpmethodmodule, httppostwebapp"/>6     </modules>7   </system.webserver>8 </configuration>
经过测试,是可以达到我们的要求(关于测试结果不在做演示)。
三、mvc过滤器在mvc中,可以通过全局的过滤器来控制请求。 
 1     public class httppostfilter : iauthorizationfilter 2     { 3         public void onauthorization(authorizationcontext filtercontext) 4         { 5             if (filtercontext.httpcontext.ispostmethod() == false) { 6  7                 //如果不是post请求,则返回404。 8                 filtercontext.result = new httpnotfoundresult(); 9             }10         }11     }
在程序启动时,注册为全局过滤器。
1     public class filterconfig2     {3         public static void registerglobalfilters(globalfiltercollection filters)4         {5             filters.add(new httppostfilter());6         }7     }
四、路由约束在注册路由时,可以定义路由的约束。通过如下方式,可以将请求方式限制为post请求。
 1     public class routeconfig 2     { 3         public static void registerroutes(routecollection routes) 4         { 5             routes.maproute( 6                 name: default, 7                 url: {controller}/{action}/{id}, 8                 defaults: new { controller = home, action = index, id = urlparameter.optional } 9                 //限制请求方式必须是post10               , constraints:new { httpmethod =  new httpmethodconstraint(post)}11             );12         }13     }
五、重写controller方法在mvc中,所有控制器默认继承于controller。
我们可以定义一个basecontroller的抽象类,重写onactionexecuting,其它的控制器都继承于basecontroller。
 1     public abstract class basecontroller : controller 2     { 3         protected override void onactionexecuting(actionexecutingcontext filtercontext) 4         { 5              6             if (filtercontext.httpcontext.ispostmethod() == false) { 7                 //如果不是post请求,则返回404。 8                 filtercontext.result = new httpnotfoundresult(); 9             }10             else {11                 base.onactionexecuting(filtercontext);12             }13         }14     }
这种方法,需要修改所有控制器的基类,不推荐。
当然如果你已经定义了自己的控制器基类,这种方式的工作量也是非常小的。
总结上述五种方法中,二、三、四方法都非常简单,但是我比较推荐方法四,因为如果需求发生变化,维护工作量是最小的。
如果大家有其它的方式,请留言,谢谢!
以上就是mvc 5限制所有http请求必须是post方式的详细内容。
其它类似信息

推荐信息