2009-07-02 6 views
3

AcceptVerbsAttributeを追加することで、特定のActionResultメソッドが応答するHTTPメソッドを制限することができます。ActionResult AcceptVerbsAttributeデフォルトHTTPメソッドとは何ですか?

[AcceptVerbs(HttpVerbs.Get)] 
public ActionResult Index() { 
    ... 
} 

しかし、私は思っていた:のActionResultメソッドは明示[AcceptVerbs(...)]属性なしを受け入れるHTTPメソッド?

は、私はそれが、HEADPOSTをGET だったが、ちょうどダブルチェックしたいと思ったと推定ます。

ありがとうございました。

+0

私の推測は(PUT、DELETEも含めて)どんなものであろう。私はあなたが実験で見つけることができると思います - AcceptVerbs属性なしで簡単なテストアクションを行い、それに対するいくつかの異なるリクエストを投げ、何が起こるかを見てください。私は答えを知りたいです:-) –

答えて

5

AcceptVerbsAttributeを指定しないと、Actionは任意のHTTPメソッドでリクエストを受け付けます。あなたのRouteTableのHTTPメソッドを制限することができます:

routes.MapRoute(
    "Default",            // Route name 
    "{controller}/{action}/{id}",       // URL with parameters 
    new { controller = "Home", action = "Index", id = "" }, // Parameter defaults 
    new { HttpMethod = new HttpMethodConstraint(
     new[] { "GET", "POST" }) }       // Only GET or POST 
); 
+0

RouteTable経由でHTTPメソッドを制限することはできませんでした。ありがとう。 –

3

すべてのHTTPメソッドを受け入れます。属性なしだから、

private static List<MethodInfo> RunSelectionFilters(ControllerContext 
    controllerContext, List<MethodInfo> methodInfos) 
{ 
    // remove all methods which are opting out of this request 
    // to opt out, at least one attribute defined on the method must 
    // return false 

    List<MethodInfo> matchesWithSelectionAttributes = new List<MethodInfo>(); 
    List<MethodInfo> matchesWithoutSelectionAttributes = new List<MethodInfo>(); 

    foreach (MethodInfo methodInfo in methodInfos) 
    { 
     ActionMethodSelectorAttribute[] attrs = 
      (ActionMethodSelectorAttribute[])methodInfo. 
       GetCustomAttributes(typeof(ActionMethodSelectorAttribute), 
        true /* inherit */); 

     if (attrs.Length == 0) 
     { 
      matchesWithoutSelectionAttributes.Add(methodInfo); 
     } 
     else 
      if (attrs.All(attr => attr.IsValidForRequest(controllerContext, 
       methodInfo))) 
      { 
       matchesWithSelectionAttributes.Add(methodInfo); 
      } 
    } 

    // if a matching action method had a selection attribute, 
    // consider it more specific than a matching action method 
    // without a selection attribute 
    return (matchesWithSelectionAttributes.Count > 0) ? 
     matchesWithSelectionAttributes : 
     matchesWithoutSelectionAttributes; 
} 

マッチングアクションメソッドは、明示的な属性では良いがある場合は、アクションメソッドになります。

ActionMethodSelector.csからわずかフォーマットされた断片(ASP.NET MVCソースがhereをダウンロードすることができます)を見利用される。

関連する問題