1

有没有办法将 global.asax 文件编译或预编译为 dll 并在 bin 文件夹中使用它?

我在这个文件中有一个许可逻辑,其他文件不会由我编译。

我还可以检查 dll 本身是否存在于 bin 文件夹中。

void Application_BeginRequest(object sender, EventArgs e)
    {
       //Application is allowed to run only on specific domains
       string[] safeDomains = new string[] { "localhost" };
       if(!((IList)safeDomains).Contains(Request.ServerVariables["SERVER_NAME"]))
       {
           Response.Write("Thisweb application is licensed to run only on: "
           + String.Join(", ", safeDomains));
           Response.End();
       }
    }
4

1 回答 1

3

您可以通过在 Application 指令上指定 Inherits 属性来将代码与 global.asax 文件分开。现在您不必在 Global.asax 文件中编写代码。

<%@ Application Inherits="Company.LicensedApplication" %>

事实上,这是 Global.asax 中唯一需要的代码行。相反,您需要一个单独的 C# 文件来为您的应用程序类编写代码:

namespace Company
{
    public class LicensedApplication : System.Web.HttpApplication
    {
        void Application_BeginRequest(object sender, EventArgs e)
        {
            // Check license here
        }
    }
}

现在您可以在 bin 文件夹中安装带有已编译应用程序类的 Web 应用程序。

于 2011-12-09T00:18:28.937 回答