一种公开的行动方法。“在控制器上没有发现”。

时间:2022-10-28 15:01:18

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

当我尝试用正确的用户名和密码登录时,这个方法很有效。所使用的是循环的,每个页面都包含一个随机的图像。但是当我给出用户名和密码的无效组合时。共享布局页面没有找到我要用@Html调用的控制器。操作和实际登录视图应该返回一个错误消息“无效的用户名和密码组合”,当然还有随机图像。

InnerException:

InnerException:

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

{“在控制器的项目上没有找到一个公共的行动方法‘随机发起者’”。

My Html.Action in shared layout.

我的Html。行动在共享布局。

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

Method in homecontroller.

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

的问候。

5 个解决方案

#1


63  

If the executing request is a POST, then it will try to find a method RandomSponsor accepting HttpPost. If makes sense, you could remove HttpGet and that should do the trick.

如果执行的请求是POST,那么它将尝试查找接受HttpPost的RandomSponsor方法。如果有意义,您可以删除HttpGet,这应该可以实现。

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方法

#2


5  

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的部分视图),那么调用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’上找不到‘public action method’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)]

[AcceptVerbs HttpVerbs。得到| HttpVerbs.Post)]

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

请记住[HttpGet]与[accept谓词(http动词:get)相同。

#3


0  

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的安装时,突然在几个不同的部分视图(不是所有视图)上收到了这个错误。我们没有对这些错误的方法或观点做出任何改变,所以对于它们为什么会被破坏,这是非常令人沮丧的。

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.

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

#4


0  

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(“行动”,“控制器”,FormMethod.Post))

fixed this issue.

解决这一问题。

#5


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

你用Html.RenderAction给它打了一针吗?它通常比Html更快。操作,因为它直接与响应流交互,而不是构建字符串。

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

另一件需要注意的事情是Html。行动或Html。RenderAction,您的视图不需要在共享文件夹中,这只需要使用Html。部分或Html.RenderPartial

#1


63  

If the executing request is a POST, then it will try to find a method RandomSponsor accepting HttpPost. If makes sense, you could remove HttpGet and that should do the trick.

如果执行的请求是POST,那么它将尝试查找接受HttpPost的RandomSponsor方法。如果有意义,您可以删除HttpGet,这应该可以实现。

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方法

#2


5  

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的部分视图),那么调用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’上找不到‘public action method’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)]

[AcceptVerbs HttpVerbs。得到| HttpVerbs.Post)]

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

请记住[HttpGet]与[accept谓词(http动词:get)相同。

#3


0  

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的安装时,突然在几个不同的部分视图(不是所有视图)上收到了这个错误。我们没有对这些错误的方法或观点做出任何改变,所以对于它们为什么会被破坏,这是非常令人沮丧的。

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.

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

#4


0  

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(“行动”,“控制器”,FormMethod.Post))

fixed this issue.

解决这一问题。

#5


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

你用Html.RenderAction给它打了一针吗?它通常比Html更快。操作,因为它直接与响应流交互,而不是构建字符串。

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

另一件需要注意的事情是Html。行动或Html。RenderAction,您的视图不需要在共享文件夹中,这只需要使用Html。部分或Html.RenderPartial