2011-10-17 66 views
1

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

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

我也可以检查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(); 
     } 
    } 
+2

这是否比编译你的许可证代码到一个程序集并引用来自global.asax(和/或web.config)更好?我认为那里有实用的东西,所以他们不能只是删除它,没有它的工作? – Rup

+0

反正人们可以反编译它,但这种人非常有限:) –

+0

@HasanGürsoy你有没有时间尝试我建议的方法? –

回答

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应用程序。

+0

我们如何触发从任何页面编译此文件 – Learning