A public action method '..' was not found on controller '..'

╄→尐↘猪︶ㄣ 提交于 2019-11-29 02:48:22
Claudio Redi

If the executing request is a POST, then it will try to find a method RandomSponsor accepting HttpPost. If this makes sense, you could remove HttpGet and that should do the trick.

Mauricio Gracia Gutierrez

This can also happen if you have many layers of calls that start with a POST (I had an action returning a view returning a partial view calling RenderAction), then the call to RenderAction will still look for a POST method

Very similar to this problem that I had here - How to solve "public action method 'methodActionName' was not found on controller 'controllerNameController'"

And if you want to continue to accept the HTTP GET verb and fix the problem of cascading post request into a get request add this to your method

[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]

Keep in mind that [HttpGet] is the same as [AcceptVerbs(HttpVerbs.Get)]

Received this error all of the sudden on several different PartialViews (not all of them) when customizing an install of MVCForum. We had not made any changes to the methods or views concerning the errors so it was really frustrating as to why they were broken.

After trying the other solutions on this post and others, went back through the changes made and what ended up stopping the errors was that we had changed the bindings in IIS to another domain that had the 'enforce lower case url' URL Rewrite rule enabled.

When we disabled the enforce lowercase rule, the errors stopped and the site worked as it was supposed to. It's not a URL Rewrite issue (I don't think) because we are able to enforce www using it with no errors. It's a lowercase rewrite issue. Didn't matter if we had the lowercase rule before or after the www rule.

This solution probably doesn't apply to many cases of this error, but it worked for us. Hopefully someone else can benefit from such a simple fix.

In my case the same issue was happening randomly with the implicit

using(Html.BeginForm())

Changing above to

using (Html.BeginForm("Action","Controller", FormMethod.Post))

fixed this issue.

I know this is a pretty old thread - but as it's top Google result I thought I'd add a potentially missing link for MVC.Net 5.2.6.

Scenario

I was attempting to call a child action via @Html.Action("ActionName", new { Id = 123}) and received an error much like the above, but none of the other solutions worked. I could hit the controller action externally (i.e. HttpGet), but the child action kept throwing the exception and was driving me nuts!

The solution I found

After two-ing and fro-ing for some time, I started playing with my routing attributes. I had the controller set up as:

[Route("{action}")]
[RoutePrefix("Prefix")]
[RouteArea("AreaName")]

As there was only one public action i wanted, "Index", I removed the {action} and placed an explicit route attribute on the public action and put my ChildActionOnly attribute back on the child.

After I did that, I hit the run and hey presto - the action was hit.

Might be worth a try if you're getting this error while using attribute routing. Note I did attempt to route the child action and this didn't work.

This will happen if the request is a POST but the controller method is annotated [HttpGet]. For example, you might issue a POST that returns a view containing partial views called with @Html.Action, using controller methods annotated with [HttpGet]. If the original request is a POST, all of the controller methods subsequently called will need to support POST.

To fix it you can use the AcceptVerbs attribute to specify that your controller method accepts both POST and GET:

[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]
Pierluc SS

Did you give it a shot with Html.RenderAction? It is typically faster then Html.Action as it interact directly into the response stream as opposed to building a string.

You can view the following topics for more info:

Another thing to note is that for Html.Action or Html.RenderAction, your view doesn't need to be in Shared folder, that is only required if you use Html.Partial or Html.RenderPartial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!