我最近将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的构建操作作为内容。
如果我在这里做错了,请告诉我。