【发布时间】:2013-03-13 03:37:42
【问题描述】:
我已经在网上寻找了几个小时的答案,所以我希望这里有人可以提供帮助。
我在 VS 2010(.Net 4.0 框架,C#)中有一个网站,在根目录中有一个 Global.asax 文件,在 App_Code 文件夹中有一个 Global.asax.cs 文件。 (请注意,这是一个网站,而不是 Web 应用程序)。当从我的本地开发机器上运行这个网站时,一切都按预期运行——更具体地说,Global.asax.cs 文件中的 Session_Start 事件会触发并在每个会话开始时运行一些代码来获取用户的登录凭据,这决定了他们什么允许根据其安全级别在应用程序中查看/编辑。
但是,一旦我将此网站发布到 Windows 2003 R2 服务器上,Global.asax.cs 文件中的任何事件都不会触发。 (只有 Session_Start 事件需要代码,但我也将代码放在 Application_Start 中,只是为了看看它是否会触发 - 它没有)。
这是 Global.asax 代码:
<%@ Application CodeBehind="~\App_Code\Global.asax.cs" Inherits="Global" Language="C#" %>
这是 Global.asax.cs 代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Global
/// </summary>
public partial class Global : System.Web.HttpApplication
{
public Global()
{
// TODO: Add constructor logic here
}
protected void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
protected void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
protected void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
try
{
Session["SessionSink"] = new SessionSink();
this.GetUserName();
}
catch (Exception ex)
{
string msg = "An error occurred in Global.asax::Session_Start";
ExceptionWriter ew = new ExceptionWriter();
ew.LogError(msg, ex);
}
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
}
}
基于我读过的其他答案/讨论的一些注释:一旦发布,PrecompiledApp.config 确实会出现在网站的文件夹中。此外,bin文件夹中还有App_global.asax.compiled和App_global.asax.dll。
有人知道问题可能是什么吗?为什么这可以在我的本地计算机上工作,但一旦发布到服务器就不行?是否有我缺少的 IIS 设置?
这是我第一次使用 asp.net “网站”,因此将不胜感激。谢谢。
【问题讨论】:
-
当您将 .cs 和 .aspx 文件都保存在根文件夹中然后发布时会发生什么?你试过了吗?还是一样的行为?
-
@Athar Anis - 是的,当我将 global.asax 和 global.asax.cs 都放在根目录中时(随后必须在 global.asax 文件中将 CodeBehind 更改为 CodeFile),同样的行为持续存在。它可以在我的本地机器上运行,但不能在服务器上运行。
-
这很奇怪.. 只是为了消除我的疑虑,你能不能把你的导演从 CodeBehind="~\App_Code\Global.asax.cs" 换掉。将“\”替换为“/”
-
还将继承更改为 "inherits = "YOURNameSpace.Global" '
-
只是为了确保它不是编码问题:你用什么代码测试了
Application_Start()?
标签: asp.net global-asax