2011-02-04 48 views
1

我們有一個ASP.NET MVC項目,它有多個使用相同名稱的方法。爲了區分它們,我們創建了一個ActionMethodSelectorAttribute,以查看路由並確定應該使用哪種方法。這在開發中可以正常工作,但是一旦將其部署到生產IIS 7服務器,我們就會收到此消息。ActionMethodSelectorAttribute在IIS 7上無法正常工作

System.Reflection.AmbiguousMatchException: The current request for action 'Delete' on controller type 'OperationsController' is ambiguous between the following action methods: 
System.Web.Mvc.ActionResult Delete(PermissionArea, PermissionPost) on type OperationsController 
System.Web.Mvc.ActionResult Delete(PermissionArea, PermissionPost, PermissionEntity`1[Comment]) on type OperationsController 
    at System.Web.Mvc.ReflectedControllerDescriptor.FindAction(ControllerContext controllerContext, String actionName) 
    at System.Web.Mvc.ControllerActionInvoker.FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, String actionName) 
    at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) 
    at System.Web.Mvc.Controller.ExecuteCore() 
    at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__4() 
    at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0() 
    at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
    at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) 

該屬性看起來像這樣。

public class StrictRouteMatchingAttribute : ActionMethodSelectorAttribute 
    { 
     private List<string> ignoreList = new List<string>() 
               { 
                "action", 
                "controller" 
               }; 

     private List<string> matchList = new List<string>(); 

     public StrictRouteMatchingAttribute() 
     { 

     } 

     public StrictRouteMatchingAttribute(string[] ValuesToMatch) 
     { 
      matchList.AddRange(ValuesToMatch.Select(x => x.Trim().ToLower())); 
     } 

     public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo) 
     { 
      var filteredList = controllerContext.RequestContext.RouteData.Values.Keys.Where(x => !ignoreList.Contains(x.ToLower())); 
      var matches = filteredList.Intersect(matchList); 
      var extras = filteredList.Where(x => !matchList.Contains(x.ToLower())); 

      if (matches.Count() == matchList.Count() && extras.Count() == 0) 
      { 
       return true; 
      } 

      return false; 
     } 
    } 

現在web.config已經使用system.webServer這樣配置了。

<system.webServer> 
    <validation validateIntegratedModeConfiguration="false" /> 
    <modules runAllManagedModulesForAllRequests="true"> 
     <add name="Elmah.ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" /> 
     <!--<add name="Elmah.ErrorFilter" type="Elmah.ErrorFilterModule" preCondition="managedHandler" />--> 
     <!--<add name="Elmah.ErrorMail" type="Elmah.ErrorMailModule" preCondition="managedHandler" />--> 
    </modules> 
    <handlers> 
     <add name="dotless" type="dotless.Core.LessCssHttpHandler,dotless.Core" path="*.less" verb="*" /> 
     <remove name="MvcHttpHandler" /> 
     <remove name="UrlRoutingHandler" /> 
     <add name="MvcHttpHandler" preCondition="integratedMode" verb="*" path="*.mvc" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" /> 
     <add name="Elmah" path="elmah.axd" verb="POST,GET,HEAD" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" /> 
    </handlers> 
    </system.webServer> 

任何想法,爲什麼這將工作正常使用內置的Web服務器,但出版時有問題?

謝謝。

回答

0

ActionMethodSelectorBase.RunSelectionFilters將返回所有匹配,所以你需要添加額外的行動可以使其他屬性匹配無效的屬性。

在我的場景中,我有1使用自定義ActionMethodSelectorAttribute 1 [HttpPost]和2 [HttpGet]操作,從默認方法中刪除[HttpGet]足以沒有它由操作篩選器返回,它是未發佈或我的選擇器失效時的默認值。