未在区域中调用 MVC 控制器
MVC controller not called in Area
我有一个无法正常工作的 MVC 控制器。
在一个页面上,我有一个表单,在该表单的某处有一个操作 link:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<table>
<tr>
<th>
@Html.ActionLink("Neues Gastkonto anlegen", "Index", "NewGuest", null, new { @class="button"})
</th>
</tr>
...
</table>
}
link 正确地呈现为 <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");
}
但是,点击第一页的link并没有抛出异常,而是告诉我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 的评论:
"Turns out, it was my code, in order to inject the serviceFactory, I had to overwrite GetControllerInstance
of DefaultControllerFactory
. I messed it up so that both TutorController
(primary page) and NewGuestController
returned an instance of TutorController
, thus the NewGuestController
would never be reached."
我有一个无法正常工作的 MVC 控制器。
在一个页面上,我有一个表单,在该表单的某处有一个操作 link:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<table>
<tr>
<th>
@Html.ActionLink("Neues Gastkonto anlegen", "Index", "NewGuest", null, new { @class="button"})
</th>
</tr>
...
</table>
}
link 正确地呈现为 <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");
}
但是,点击第一页的link并没有抛出异常,而是告诉我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 的评论:
"Turns out, it was my code, in order to inject the serviceFactory, I had to overwrite GetControllerInstance
of DefaultControllerFactory
. I messed it up so that both TutorController
(primary page) and NewGuestController
returned an instance of TutorController
, thus the NewGuestController
would never be reached."