2012-10-25 53 views
3

基本上我们分发给客户端的一个web应用程序,其中一个将试用它,所以我需要能够在某个特定点关闭它。不想把结束日期放在web.config中,以防万一他们可以改变它,我正在考虑在global.asax中加入一些硬编码的日期,但是我不确定我可以'关闭'应用程序。我正在考虑在验证请求部分中检查日期,并简单地重定向到说明您的试验已完成(或类似情况)的页面,但有没有更好的方法?使一个asp.netnet应用程序离线

+0

你的意思是说,你会不会收留了它与您的客户将托管代码本身? – Ramesh

+0

该应用安装在客户端服务器上 – user1166905

+0

如果您尝试创建'app_offline.html',则用户可以删除在该处创建文件的权限,这样他们就可以避免这种情况。 – Aristos

回答

3

你可以做到这一点上global.asax为:

protected void Application_BeginRequest(Object sender, EventArgs e) 
{ 
    if(DateTime.UtcNow > cTheTimeLimitDate) 
    { 
     HttpContext.Current.Response.TrySkipIisCustomErrors = true; 
     HttpContext.Current.Response.Write("...message to show..."); 
     HttpContext.Current.Response.StatusCode = 403; 
     HttpContext.Current.Response.End(); 
     return ;  
    }  
} 

这比其放置在web.config中更安全,但没有什么是足够安全的。它甚至更好地将它们重定向到一个页面,或者不向他们显示消息,或者你认为的任何东西。

对于化妆重定向到一个页面,你还需要检查,如果呼叫如果一个页面,该代码如下:

protected void Application_BeginRequest(Object sender, EventArgs e) 
{ 
    string cTheFile = HttpContext.Current.Request.Path; 
    string sExtentionOfThisFile = System.IO.Path.GetExtension(cTheFile); 
    if (sExtentionOfThisFile.Equals(".aspx", StringComparison.InvariantCultureIgnoreCase)) 
    { 
    // and here is the time limit. 
    if(DateTime.UtcNow > cTheTimeLimitDate) 
    { 
     // make here the redirect 
     HttpContext.Current.Response.End(); 
     return ;  
    }  
    } 
} 

要真是难上加难,你可以自定义BasePage的那所有页面都来自它(而不是来自System.Web.UI.Page),并且您在页面渲染上放置了限制 - 或者在每个页面渲染的顶部显示一条消息,结束时间。

public abstract class BasePage : System.Web.UI.Page 
{ 
    protected override void Render(System.Web.UI.HtmlTextWriter writer)   
    { 
     if(DateTime.UtcNow > cTheTimeLimitDate) 
     { 
      System.IO.StringWriter stringWriter = new System.IO.StringWriter(); 

      HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter); 

      // render page inside the buffer 
      base.Render(htmlWriter); 

      string html = stringWriter.ToString(); 

      writer.Write("<h1>This evaluation is expired</h1><br><br>" + html);   
     } 
     else 
     { 
      base.Render(writer); 
     } 
    } 
} 
0

只需添加app_offline.htm,您甚至可以为您的用户创建一条好消息。此外,将网站重新联机非常容易,只需删除或重命名app_offline.htm即可。

http://weblogs.asp.net/dotnetstories/archive/2011/09/24/take-an-asp-net-application-offline.aspx

+0

他想给用户一个试用类型的应用程序。因为任何ASP.NET开发人员都应该知道删除app_offline.htm将使网站恢复在线状态,所以这对他来说不会有好处。 – Ramesh

+0

哦..是的,你是对的..我错过了关于审判的部分。 – mboldt