gpt4 book ai didi

c# - 路径 '/' 的 Controller 未找到或未实现 IController

转载 作者:行者123 更新时间:2023-11-30 13:38:24 26 4
gpt4 key购买 nike

我正在编写一段代码,在每个网页上显示随机赞助商图片。我认为调用我的函数的最佳位置是在 Views/Shared/_Layout.cshtml 页面中,因为这是在每个页面加载的地方。

我在我的域模型中的服务类中编写了该函数,并在我的家庭 Controller 中编写了一个 ChildActionOnly 函数,在 Views/Home/Randomsponsor.cshtml 中的一个简单 View 中返回值并调用使用 Html.action 在共享布局中发挥作用。

一切正常,但在运行时出现下一个错误:

{"The controller for path '/' was not found or does not implement IController."}

有谁知道如何解决这个问题?

领域项目中的方法:

public String advertsForCountry()
{
String studentSchool = finder.getLoggedStudent().SchoolId;
int studentCountry = db.Schools.Find(studentSchool).CountryId;

List<Sponsor> sponsorsForStudent = new List<Sponsor>();
List<Advert> adverts = db.Adverts.ToList();
foreach(Advert adv in adverts)
{
foreach(Country cntry in adv.Countries)
{
if(cntry.CountryId == studentCountry)
{
sponsorsForStudent.Add(adv.Sponsor);
}
}
}
Random random = new Random();
int randomSP = random.Next(0, sponsorsForStudent.Count()-1);
string sponsorAdvert = sponsorsForStudent.ElementAt(randomSP).SponsorCompany;
return sponsorAdvert;
}

在家庭 Controller 中:

[HttpGet]   
[ChildActionOnly]
public ActionResult RandomSponsor()
{
var model = service.advertsForCountry();

return PartialView("RandomSponsor", model);
}

Views/Home/中的简单 View :

@{
ViewBag.Title = "RandomSponsor";
}

@Html.Action("RandomSponsor")

我在 View/Shared/_Layout.cshtml 中调用的函数包含导航栏等:

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

问候。

最佳答案

(转自评论)

您不需要在 Html.Action 中指定 Controller 的完整类名。尝试使用 @Html.Action("RandomSponsor", "Home") 而不是 @Html.Action("RandomSponsor", "HomeController")

关于c# - 路径 '/' 的 Controller 未找到或未实现 IController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16923546/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com