58

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
Gijs
  • 885
  • 2
  • 13
  • 22
  • can you post your route table definition? (RegisterRoutes in global.asax) and also controller definition which has RandomSponsor() action. – Pierluc SS Jun 05 '13 at 15:42
  • I opened a bug for this https://aspnetwebstack.codeplex.com/workitem/2295 – Chris Marisic Feb 24 '16 at 15:54
  • I was getting the same error using Mozilla Firefox 45. Doing the same thing with Google Chrome 48.0.2564.116 m, the problem is gone. In my case there is some JavaScript involved and I suspect that is the problem. – JayJay Mar 10 '16 at 21:46

8 Answers8

100

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.

John Lord
  • 1,941
  • 12
  • 27
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
  • 10
    Ok, I've seen this and it didn't register at first; 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. I hope it will prevent someone else from spending four hours on this :) – Marcel Popescu Jan 24 '14 at 09:44
  • 2
    How to fix this problem not removing `[HttpGet]` attribute? I want to send route values to the method by GET, not POST. – pt12lol Jan 27 '14 at 13:41
  • Oh I found solution. View was called by POST and that excluded loading `@Html.Action()` by GET. – pt12lol Jan 27 '14 at 14:41
  • @MarcelPopescu Thanks – FindOutIslamNow May 09 '18 at 12:45
19

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

Community
  • 1
  • 1
Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
7

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)]
Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
2

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.

swimex
  • 21
  • 5
2

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.

Greg Quinn
  • 1,927
  • 1
  • 23
  • 26
0

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.

DanB
  • 2,022
  • 1
  • 12
  • 24
tomekole
  • 371
  • 4
  • 6
0

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.

Mike C
  • 11
  • 4
-1

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

Community
  • 1
  • 1
Pierluc SS
  • 3,138
  • 7
  • 31
  • 44
  • `Html.Partial` and `Html.RenderPartial` don't need to be in the Shared folder either. If it's in the same folder as the view that calls it you can reference it directly. You can also use the full path of the view eg `~/Views/MyFolder/_Partial.cshtml` – Carl May 23 '19 at 14:58