Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 817
Gijs Avatar asked Jun 05 '13 13:06

Gijs


People also ask

How do I add actions to my controller?

You add a new action to a controller by adding a new method to the controller. For example, the controller in Listing 1 contains an action named Index() and an action named SayHello(). Both methods are exposed as actions.

Can action method be public in MVC?

In this case, the Index() method is called on the ProductController class. The Index() method is an example of a controller action. A controller action must be a public method of a controller class. C# methods, by default, are private methods.

What is a public method of a controller class?

Controller class contains public methods called Action methods. Controller and its action method handles incoming browser requests, retrieves necessary model data and returns appropriate responses. In ASP.NET MVC, every controller class name must end with a word "Controller".

What is action method in controller?

All the public methods of the Controller class are called Action methods. They are like any other normal methods with the following restrictions: Action method must be public. It cannot be private or protected. Action method cannot be overloaded.


2 Answers

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.

like image 138
Claudio Redi Avatar answered Sep 24 '22 17:09

Claudio Redi


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

like image 33
Mauricio Gracia Gutierrez Avatar answered Sep 22 '22 17:09

Mauricio Gracia Gutierrez