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

asp.net Mvcasp.net Mvc-Partialview

asp.net Mvc Problem Overview


I wanted to put a random image on every viewpage of my mvc project. So i created a method that returns a partialView and call that method in the shared Layout page.

This works fine when I try to login with a correct username and password. The used is loged in and every page contains a random image. But when I give the invalid combination of username and password. The shared layout page does not find the controller I want to call with my @Html.Action and actualy the login view should be returned with an error message 'invalid combination of username and password' and ofcourse, with the random image.

InnerException: > {"A public action method 'RandomSponsor' was not found on controller 'Project.WebUI.Controllers.HomeController'."}

My Html.Action in shared layout.

@Html.Action("RandomSponsor", "Home")

Method in homecontroller.

    [HttpGet]   
[ChildActionOnly]
public ActionResult RandomSponsor()
{
    var model = service.getRandomSponsor();
    return PartialView("RandomSponsor", model);
}

The getRandomSponsor method works fine, this one always returns one random string value that is returned to the RandomSponsor.cshtml view.

RandomSponsor.schtml (only contains the image string)

<img src="~/Content/Images/Advert/@(Model)" alt="a" />

I searched the web for this problem but didn't found a solution, does anyone know the answer to this one? Might it be something with HttpGet of HttpPost?

Regards.

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

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.

Solution 2 - asp.net Mvc

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 - https://stackoverflow.com/questions/34646323/how-to-solve-public-action-method-methodactionname-was-not-found-on-controlle/34646413?noredirect=1#comment57039046_34646413

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)]

Solution 3 - asp.net Mvc

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)]

Solution 4 - asp.net Mvc

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.

Solution 5 - asp.net Mvc

I just solved this issue strangely enough on my local PC, by making sure my entire request path was lower case. So give that a try.

Solution 6 - asp.net Mvc

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.

Solution 7 - asp.net Mvc

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.

Solution 8 - asp.net Mvc

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGijsView Question on Stackoverflow
Solution 1 - asp.net MvcClaudio RediView Answer on Stackoverflow
Solution 2 - asp.net MvcMauricio Gracia GutierrezView Answer on Stackoverflow
Solution 3 - asp.net MvcOwen PaulingView Answer on Stackoverflow
Solution 4 - asp.net MvcswimexView Answer on Stackoverflow
Solution 5 - asp.net MvcGreg QuinnView Answer on Stackoverflow
Solution 6 - asp.net MvctomekoleView Answer on Stackoverflow
Solution 7 - asp.net MvcMike CView Answer on Stackoverflow
Solution 8 - asp.net MvcPierluc SSView Answer on Stackoverflow