c# - 未在区域中调用 MVC Controller

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

我有一个无法正常工作的 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"})

MSDN Reference

更新

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

“事实证明,这是我的代码,为了注入(inject) serviceFactory,我不得不覆盖 DefaultControllerFactoryGetControllerInstance。我搞砸了,所以 TutorController(主页面)和 NewGuestController 返回了 TutorController 的实例,因此永远不会到达 NewGuestController。”

关于c# - 未在区域中调用 MVC Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28579386/

相关文章:

c# - 在 C# WPF 中使用 Datacontext 填充数据网格

asp.net - 在 ASP.NET 中总计 GridView

Asp.net MVC 何时在丢失时重新加载声明

asp.net-mvc - 在 ServiceStack 服务中获取请求 URL、方案、主机名和端口

c# - 从代码指标中排除测试代码

c# - 空旗是坏习惯吗?

c# - Excel 自动化 : How do I set the range?

c# - Web 应用程序中的确认消息框

c# - 如何创建一个 "Processing"页面?

asp.net-mvc - 欧文记得浏览器到底是做什么的?