ASP.NET MVC应用程序控制器的构造函数问题 [英] ASP.NET MVC application controller constructor problem

查看:91
本文介绍了ASP.NET MVC应用程序控制器的构造函数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个应用程序控制器抽象类,我控制器派生自(如下面的文章

I've created an application controller abstract class that my controllers derive from (As described in the following article)

以下是我的什么code看起来像

The following is an example of what my code looks like

public abstract class ApplicationController : Controller
{
    private ProjectDataContext datacontext = new ProjectDataContext();

    protected ProjectDataContext DataContext
    {
        get { return datacontext; }
    }

    public ApplicationController()
    {
        ViewData["OpenTasks"] = DataContext.Tasks.Where(t => t.UserId == this.UserId).Count();
    }
}

这会产生,我已经确定了以下错误是由于去哪儿LAMDA前pression:

This produces the following error which i have determined is due to the "Where" lamda expression:

如果控制器没有一个控制器工厂,确保它有一个无参数的公共构造函数。

If the controller doesn't have a controller factory, ensure that it has a parameterless public constructor.

这个错误产生我写任何方式LINQ查询和编译应用程序的唯一方法就是通过如下删除去哪儿的条款。

this error is produced whichever way i write the LINQ query and the only way to compile the application is by removing the "Where" clause as follows.

ViewData["OpenTasks"] = DataContext.Tasks.Count();

任何想法,我需要对用户执行查询并不会返回所有条目是什么问题或如何解决这个问题。

any ideas what the problem is or how to resolve this as i need to execute the query against the user and not return all entries.

在此先感谢

推荐答案

试试这个,而不是使用构造函数: -

Try this instead of using the constructor:-

public abstract class ApplicationController : Controller
{
    private ProjectDataContext datacontext = new ProjectDataContext();

    protected ProjectDataContext DataContext
    {
        get { return datacontext; }
    }

    protected override void Initialize(System.Web.Routing.RequestContext requestContext)
    {
        base.Initialize(RequestContext);
        ViewData["OpenTasks"] = DataContext.Tasks.Where(t => t.UserId == this.UserId).Count();
    }
}

它很可能当前的用户ID dependand在RequestContext的

Its quite likely that the current user ID is dependand on the RequestContext

这篇关于ASP.NET MVC应用程序控制器的构造函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆