我有一个无法正常工作的 MVC Controller 。
在一个页面上,我有一个表单,其中某处有一个操作链接:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<table>
<tr>
<th>
@Html.ActionLink("Neues Gastkonto anlegen", "Index", "NewGuest", null, new { @class="button"})
</th>
</tr>
...
</table>
}
该链接正确呈现为 <host>/GluexDB/NewGuest
.请注意,我在 GluexDB 区域工作,并且我的路由已正确设置。
NewGuestController如下:
public class NewGuestController : Controller
{
IGluexDBServiceFactory serviceFactory;
//used for injecting the database
public NewGuestController(IGluexDBServiceFactory serviceFactory)
{
if (serviceFactory == null)
{
throw new Exception("Please provide a valid serviceFactory");
}
this.serviceFactory = serviceFactory;
}
public ActionResult Index()
{
throw new NotImplementedException("CnG Cont"); //<-- no exception thrown here
return View("CreateNewGuest");
}
但是,单击第一页中的链接不会引发异常,而是告诉我 The view 'Index' or its master was not found or no view engine supports the searched locations.
即使不抛出异常,搜索到的View不应该命名为CreateNewGuest
吗? ?
为了完整起见,这是路由配置:
public class GluexDBAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "GluexDB";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"GluexDB_default",
"GluexDB/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
感谢您为我指明正确的方向!
最佳答案
需要添加区域的路由参数如下:
@Html.ActionLink("Neues Gastkonto anlegen", "Index", "NewGuest",
new { area = "GluexDB"}, new { @class="button"})
更新
解决方案如下来自@Thaoden 的评论:
“事实证明,这是我的代码,为了注入(inject) serviceFactory,我不得不覆盖 DefaultControllerFactory
的 GetControllerInstance
。我搞砸了,所以 TutorController
(主页面)和 NewGuestController
返回了 TutorController
的实例,因此永远不会到达 NewGuestController
。”
关于c# - 未在区域中调用 MVC Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28579386/