Intermittent asp.net mvc exception: “A public action method ABC could not be found on controller XYZ.”

后端 未结 13 684
春和景丽
春和景丽 2020-12-02 04:57

I\'m getting an intermittent exception saying that asp.net mvc can’t find the action method. Here’s the exception:

A public action method \'Fill\' cou

相关标签:
13条回答
  • 2020-12-02 05:33

    From the IIS logs our problem was caused by Googlebot attempting POST and a GET for a POST only controller action.

    For this case I recommend handling the 404 like Dmitriy suggestion.

    0 讨论(0)
  • 2020-12-02 05:34

    We found the answer. We looked into our web logs. It showed that we were receiving some weird http actions (verbs/methods) like OPTIONS, PROPFIND and HEAD.

    This seems to the cause of some of theses exceptions. This explains why it was intermittent.

    We reproduced the issue with the curl.exe tool:

    curl.exe -X OPTIONS http://localhost/v2.3.1.0/(S(boztz1aquhzurevtjwllzr45))/Form/Fill/273
    curl.exe -X PROPFIND http://localhost/v2.3.1.0/(S(boztz1aquhzurevtjwllzr45))/Form/Fill/273
    curl.exe -X HEAD http://localhost/v2.3.1.0/(S(boztz1aquhzurevtjwllzr45))/Form/Fill/273
    

    The fix we used was to add an authorization section to web.config:

    <authorization>
      <deny users="*" verbs="OPTIONS, PROPFIND, HEAD"/>
    </authorization>
    
    0 讨论(0)
  • 2020-12-02 05:36

    I'v got the same problem in asp.net mvc. this error - 404 not found. I resolve problem this way - put this code in MyAppControllerBase (MVC)

        protected override void HandleUnknownAction(string actionName)
        {
            this.InvokeHttp404(HttpContext);
        }
    
        public ActionResult InvokeHttp404(HttpContextBase httpContext)
        {
            IController errorController = ObjectFactory.GetInstance<PagesController>();
            var errorRoute = new RouteData();
            errorRoute.Values.Add("controller", "Pages");
            errorRoute.Values.Add("action", "Http404");
            errorRoute.Values.Add("url", httpContext.Request.Url.OriginalString);
            errorController.Execute(new RequestContext(
                 httpContext, errorRoute));
    
            return new EmptyResult();
        }
    
    0 讨论(0)
  • 2020-12-02 05:38

    The currently accepted answer does work as expected but isn't the primary use case for the feature. Instead use the feature defined by ASP.NET. In my case, I denied everything but GET and POST:

      <system.webServer>
      <security>
          <requestFiltering>
              <verbs allowUnlisted="false">
                  <add verb="GET" allowed="true"/>
                  <add verb="POST" allowed="true"/>
              </verbs>
          </requestFiltering>
      </security>
     </system.webServer>
    

    With the code snippet above, MVC will correctly return a 404

    0 讨论(0)
  • 2020-12-02 05:40

    See if simply browsing to the URL in question is enough to reproduce the error. It would if the action was only defined as a POST action. Doing this allows you to reproduce the error at will.

    In any case, you can globally handle the error as below. Another answer here that references HandleUnknownAction only handles URLs with bad action names, not bad controller names. The following approach handles both.

    Add this to your base controller (view code omitted here):

    public ActionResult Error(string errorMessage)
    {
        return View("Error");  // or do something like log the error, etc.
    }
    

    Add a global exception handler to Global.asax.cs that calls the method above or does whatever else you want to do with the caught 404 error:

    void Application_Error(object sender, EventArgs e)
    {
        Exception ex = Server.GetLastError();  // get the exception object
        HttpException httpException = ex as HttpException;
    
        if (httpException != null && httpException.GetHttpCode() == 404)  // if action not found
        {
            string errorMessage = "The requested page was not found.";
    
            RouteData routeData = new RouteData();
            routeData.Values.Add("controller", "Base");
            routeData.Values.Add("action", "Error");
            routeData.Values.Add("errorMessage", errorMessage);
    
            Server.ClearError();
            Response.TrySkipIisCustomErrors = true;
    
            // Go to our custom error view.
            IController errorController = new BaseController();
            errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
        }
    }
    
    0 讨论(0)
  • 2020-12-02 05:41

    I too had this issue.

    In my case it was related to verb restrictions on the requested action, where the view was a POST but the partial view being requested within supported GET and HEAD only. Adding the POST verb to the AcceptVerbsAttribute (in MVC 1.0) resolved the problem.

    0 讨论(0)
提交回复
热议问题