区域中未调用MVC控制器

时间:2015-02-18 08:50:43

标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor

我有一个MVC控制器,我无法正常工作。

在一个页面上,我有一个表单,其中包含某种形式的动作链接:

@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.即使没有抛出任何异常,也不应该将搜索到的视图命名为{{ 1}}?

为完整起见,这是路线配置:

CreateNewGuest

感谢您指出我正确的方向!

1 个答案:

答案 0 :(得分:7)

您需要为该区域添加路由参数,如下所示:

@Html.ActionLink("Neues Gastkonto anlegen", "Index", "NewGuest", 
                        new { area = "GluexDB"}, new { @class="button"})

<强> MSDN Reference

<强>更新

解决方案如下:@ Thaoden的评论:

“事实证明,这是我的代码,为了注入serviceFactory,我不得不覆盖GetControllerInstance的{​​{1}}。我搞砸了DefaultControllerFactory(主页)并且TutorController返回了NewGuestController的实例,因此永远无法访问TutorController。“