【问题标题】:Parser Error Message: Could not load type "ContextModule"解析器错误消息:无法加载类型“ContextModule”
【发布时间】:2012-09-22 22:22:38
【问题描述】:

我刚开始学习asp.net MVC3,遇到了这个问题:

我使用的是 Visual Studio 2010,在构建过程中没有错误,只有当我尝试运行应用程序时。 我正在谷歌上寻找答案,但没有成功。 有人知道如何解决这个问题吗?

谢谢!

EDIT-ContextModule 代码:

using System;
using System.Web;

namespace testbaza.Models
{
    public class ContextModule : IHttpModule
    {

        internal const string CONTEXT_KEY = "datacontext";

        public void Dispose()
        {

        }

        public void Init(HttpApplication context)
        {
            context.PostRequestHandlerExecute += new EventHandler(context_PostRequestHandlerExecute);
            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
        }

        private void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session != null)
            {
                HttpContext.Current.Session[CONTEXT_KEY] = new EntitiesModel();
            }
        }

        private void context_PostRequestHandlerExecute(object sender, EventArgs e)
        {
            CommitTransactions();

            DisposeContext();

            ClearSession();

        }

        private void CommitTransactions()
        {
            if (HttpContext.Current.Session == null)
            {
                return;
            }

            EntitiesModel dbContext =
                HttpContext.Current.Session[CONTEXT_KEY] as EntitiesModel;
            if (dbContext != null)
            {

                dbContext.SaveChanges();
            }
        }

        private void DisposeContext()
        {
            if (HttpContext.Current.Session == null)
            {
                return;
            }

            EntitiesModel dbContext =
                HttpContext.Current.Session[CONTEXT_KEY] as EntitiesModel;
            if (dbContext != null)
            {

                dbContext.Dispose();
            }
        }

        private void ClearSession()
        {

            if (HttpContext.Current.Session == null)
            {
                HttpContext.Current.Session.Remove(CONTEXT_KEY);
            }
        }
    }
}

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-3 razor


    【解决方案1】:

    您似乎附加了一个找不到的 HTTP 模块。您是如何创建项目的? 该模块可以在 web.config 中删除。

    编辑
    你需要改变:

    <httpModules>
      <add name="ContextModule" type="testbaza.ContextModule, testbaza" />
    </httpModules>
    

    <httpModules>
      <add name="ContextModule" type="testbaza.Models.ContextModule, testbaza" />
    </httpModules>
    

    注意更改的命名空间。

    【讨论】:

    • 我已经创建了模块和项目,如视频中所示tv.telerik.com/watch/orm/…
    • @user1598696 你能发布 ContextModule 的源代码吗?只是为了能够检查命名空间等?
    • @user1598696 更新了答案
    【解决方案2】:

    我认为您应该从您的 add 标记中删除 , testbaza 部分。

    <configuration>
        <system.web>
            <httpModules>
                <add name="MyModule" type="MyModule" />
            </httpModules>
        </system.web>
    </configuration>
    

    【讨论】:

      猜你喜欢
      • 2020-06-15
      • 2019-11-02
      • 2021-11-14
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      • 2012-07-25
      • 2022-01-15
      • 1970-01-01
      相关资源
      最近更新 更多