开发者

Need to temporarily disable access to ASP.Net MVC 3 website

开发者 https://www.devze.com 2023-02-25 15:44 出处:网络
What\'s the easiest way to temporarily disable access to ASP.Net MVC 3 website? Acce开发者_运维技巧ss is restricted to specific IP or localhost

What's the easiest way to temporarily disable access to ASP.Net MVC 3 website? Acce开发者_运维技巧ss is restricted to specific IP or localhost All external users get the text: "Matrix reloaded ..." or "Under Construction".

Thank you!


Add an app_offline.htm email to the root folder and the site will be unaccessible and any url's requested will show the app_offline.htm contents.

Edit: didn't notice the ability to be able to access locally. One way to take the site down from external users would be to add a http module to the site. The easiest (hacky) way is put the module straight in the app_code folder. something like:

using System;
using System.Web;

namespace YourNameSpace
{
 public class BlockRemoteModule : IHttpModule 
 {
  public BlockRemoteModule() {}

  public void Dispose() {}

  public void Init(HttpApplication application) 
  {
   application.BeginRequest += new EventHandler(OnBeginRequest);
  }

  public void OnBeginRequest(object sender, EventArgs e)
  {
    if (HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"] != HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"])
    {
        HttpContext.Current.Response.StatusCode = 503;
        HttpContext.Current.Response.Write("Under Construction.");
        HttpContext.Current.Response.End();
    }
  }
 }
}

And then register it in the web.config (assuming iis7+)

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
        <add name="BlockRemoteModule" type="YourNameSpace.BlockRemoteModule,App_code" />
    </modules>
</system.webServer>


You can try with action filter, that way you can choose which actions/controllers won't be accessible. This action filter could just check for certain appSetting value and if it's set to true/false it would redirect to under maintenance view.


You should configure redirection in your application server (IIS in this case):

http://technet.microsoft.com/en-us/library/cc732969(WS.10).aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号