我最近将一个 Web 表单应用程序转换为一个 mvc4 应用程序。我仍然拥有所有的 aspx 页面,但使用了 mvc 基础架构。
不过,我正面临一种奇怪的行为。当我在 VS 中发布应用程序时,会发出 global.asax,如下所示:
<%@ Application Language="C#" %><script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
void Application_BeginRequest(object sender,EventArgs e)
{
HttpContext.Current.Items["DbRepositoryFactory"] = new Bdequalizer.Model.DbRepositoryFactory();
}
void Application_EndRequest(object sender,EventArgs e)
{
var repositoryFactory = HttpContext.Current.Items["DbRepositoryFactory"] as Bdequalizer.Model.DbRepositoryFactory;
if (repositoryFactory != null)
repositoryFactory.Dispose();
}</script>
但原来的 global.asax 是:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using Bdequalizer.Model;
namespace Web
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteTable.Routes.RouteExistingFiles = false;
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
protected void Application_BeginRequest(object sender,EventArgs e)
{
HttpContext.Current.Items["DbRepositoryFactory"] = new Bdequalizer.Model.DbRepositoryFactory();
}
protected void Application_EndRequest(object sender,EventArgs e)
{
var repositoryFactory = HttpContext.Current.Items["DbRepositoryFactory"] as DbRepositoryFactory;
if (repositoryFactory != null)
repositoryFactory.Dispose();
}
}
}
现在我的问题是 Application_BeginRequest 甚至不会触发。我已经为 global.asax 构建了内容作为内容。
让我知道,如果我在这里做错了什么。