问题
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.
回答1:
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.
回答2:
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)]
回答3:
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.
回答4:
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.
回答5:
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.
回答6:
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)]
回答7:
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:
- What is the difference (if any) between Html.Partial(view, model) and Html.RenderPartial(view,model) in MVC2?
- Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction
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
来源:https://stackoverflow.com/questions/16941317/a-public-action-method-was-not-found-on-controller