Tha*_*den 1 c# asp.net asp.net-mvc razor asp.net-mvc-4
我有一个MVC控制器,我无法正常工作.
在页面上,我有一个表单,其中包含以该形式存在的操作链接:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<table>
<tr>
<th>
@Html.ActionLink("Neues Gastkonto anlegen", "Index", "NewGuest", null, new { @class="button"})
</th>
</tr>
...
</table>
}
Run Code Online (Sandbox Code Playgroud)
该链接正确呈现为<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");
}
Run Code Online (Sandbox Code Playgroud)
但是,单击第一页中的链接不会抛出异常但告诉我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 }
);
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你指点我正确的方向!
您需要为该区域添加route参数,如下所示:
@Html.ActionLink("Neues Gastkonto anlegen", "Index", "NewGuest",
new { area = "GluexDB"}, new { @class="button"})
Run Code Online (Sandbox Code Playgroud)
更新
解决方案如下:@ Thaoden的评论:
"事实证明,这是我的代码,以注入服务工厂,我不得不重写GetControllerInstance的DefaultControllerFactory.我把事情搞糟了,这样两个TutorController(主页)和NewGuestController返回的实例TutorController,因此NewGuestController永远不会到达."
| 归档时间: |
|
| 查看次数: |
291 次 |
| 最近记录: |