asp.net-mvc 在控制器“..”上找不到公共操作方法“..”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16941317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:54:19  来源:igfitidea点击:

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

asp.net-mvcasp.net-mvc-partialview

提问by Gijs

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.

我想在我的 mvc 项目的每个视图页面上放置一个随机图像。所以我创建了一个方法,它返回一个 partialView 并在共享布局页面中调用该方法。

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.Actionand actualy the login view should be returned with an error message 'invalid combination of username and password' and ofcourse, with the random image.

当我尝试使用正确的用户名和密码登录时,这工作正常。用户已登录,每个页面都包含一个随机图像。但是当我给出无效的用户名和密码组合时。共享布局页面找不到我想用我的控制器调用的控制器,实际上@Html.Action登录视图应该返回一条错误消息“用户名和密码的无效组合”,当然还有随机图像。

InnerException:

内部异常:

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

{“在控制器“Project.WebUI.Controllers.HomeController”上找不到公共操作方法“RandomSponsor”。”}

My Html.Action in shared layout.

我在共享布局中的 Html.Action。

@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.

getRandomSponsor 方法工作正常,这个方法总是返回一个随机字符串值,该值返回到 RandomSponsor.cshtml 视图。

RandomSponsor.schtml (only contains the image string)

RandomSponsor.schtml(只包含图片字符串)

<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?

我在网上搜索了这个问题,但没有找到解决方案,有人知道这个问题的答案吗?可能是 HttpPost 的 HttpGet 有问题吗?

Regards.

问候。

回答by Claudio Redi

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

如果正在执行的请求是POST,那么它将尝试找到一个RandomSponsor接受的方法HttpPost。如果这是有道理的,您可以删除HttpGet,这应该可以解决问题。

回答by 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

如果您有多个以 POST 开头的调用层(我有一个返回视图的操作返回一个调用 RenderAction 的部分视图),则也可能发生这种情况,那么对 RenderAction 的调用仍将查找 POST 方法

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

非常类似于我在这里遇到的这个问题 -如何解决“在控制器'controllerNameController'上找不到公共操作方法'methodActionName'”

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

如果您想继续接受 HTTP GET 动词并修复将 post 请求级联到 get 请求中的问题,请将其添加到您的方法中

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

[接受动词(HttpVerbs.Get | HttpVerbs.Post)]

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

请记住,[HttpGet] 与[AcceptVerbs(HttpVerbs.Get)] 相同

回答by Owen Pauling

This will happen if the request is a POSTbut the controller method is annotated [HttpGet]. For example, you might issue a POSTthat 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.

如果请求是 aPOST但控制器方法被注释,则会发生这种情况[HttpGet]。例如,您可能会使用带有注解的控制器方法发出一个POST返回包含称为 with 的部分视图的视图的视图。如果原始请求是,则随后调用的所有控制器方法都需要支持.@Html.Action[HttpGet]POSTPOST

To fix it you can use the AcceptVerbsattribute to specify that your controller method accepts both POSTand GET:

要修复它,您可以使用该AcceptVerbs属性来指定您的控制器方法同时接受POSTGET

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

回答by swimex

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.

在自定义 MVCForum 的安装时,突然在几个不同的 PartialViews(不是全部)上收到此错误。我们没有对有关错误的方法或观点进行任何更改,因此对于它们被破坏的原因真的很令人沮丧。

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.

在尝试了这篇文章和其他人的其他解决方案后,回顾了所做的更改,最终阻止错误的是我们将 IIS 中的绑定更改为另一个启用了“强制小写 URL”URL 重写规则的域。

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.

当我们禁用强制小写规则时,错误停止并且站点按预期工作。这不是 URL 重写问题(我不认为),因为我们能够强制使用 www 而不会出现错误。这是一个小写重写问题。我们在 www 规则之前或之后是否有小写规则并不重要。

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.

此解决方案可能不适用于此错误的许多情况,但它对我们有用。希望其他人可以从这样一个简单的修复中受益。

回答by tomekole

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

在我的情况下,同样的问题是随机发生的

using(Html.BeginForm())

使用(Html.BeginForm())

Changing above to

以上更改为

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

使用 (Html.BeginForm("Action","Controller", FormMethod.Post))

fixed this issue.

修复了这个问题。

回答by Mike C

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.

我知道这是一个很旧的线程 - 但由于它是 Google 的最高结果,我想我会为 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!

我试图通过调用子操作@Html.Action("ActionName", new { Id = 123})并收到与上述类似的错误,但其他解决方案均无效。我可以在外部击中控制器动作(即 HttpGet),但是子动作不断抛出异常并让我发疯!

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.

由于我只想要一个公共操作“索引”,我删除了 {action} 并在公共操作上放置了一个明确的路由属性,并将我的 ChildActionOnly 属性放回子项上。

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.

如果您在使用属性路由时遇到此错误,可能值得一试。 注意我确实尝试路由子操作,但这没有用。

回答by Pierluc SS

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

你试过了Html.RenderAction吗?它通常更快,Html.Action因为它直接与响应流交互,而不是构建字符串。

You can view the following topics for more info:

您可以查看以下主题以了解更多信息:

Another thing to note is that for Html.Actionor Html.RenderAction, your view doesn't need to be in Sharedfolder, that is only required if you use Html.Partialor Html.RenderPartial

另一件需要注意的是,对于Html.Actionor Html.RenderAction,您的视图不需要在共享文件夹中,只有在您使用Html.PartialHtml.RenderPartial