Determine if ASP.NET application is running locall

2020-02-17 08:23发布

I want to know if there is a recommended way of determining if an asp application is running locally. At the moment I use the Request object and do a string search for localhost or 127.0.0.1 on the server variable but this has several limitations. The biggest one being that the Request object is not always available when I need it.

7条回答
戒情不戒烟
2楼-- · 2020-02-17 08:47

You can check the Request.IsLocal property

查看更多
老娘就宠你
3楼-- · 2020-02-17 08:51

In a MVC view / ASP page / code behind class:

bool isLocal = HttpContext.Current.Request.IsLocal;

In an MVC controller :

bool isLocal = Request.IsLocal;
查看更多
萌系小妹纸
4楼-- · 2020-02-17 08:54

This worked for me with Application_Start

if (!HostingEnvironment.IsDevelopmentEnvironment)
{
      GlobalFilters.Filters.Add(new RequireHttpsAttribute());
}

To know more about how IsDevelopmentEnvironment is set, please look at the following thread.

In ASP.NET, what determines the value of HostingEnvironment.IsDevelopmentEnvironment?

查看更多
叛逆
5楼-- · 2020-02-17 08:58

Request.IsLocal is the same as checking for 127.0.0.1 or ::1. See this post: http://forums.asp.net/p/1065813/4081335.aspx.

查看更多
该账号已被封号
6楼-- · 2020-02-17 08:58

See HttpRequest.IsLocal

bool isLocal = HttpContext.Current.Request.IsLocal;
查看更多
smile是对你的礼貌
7楼-- · 2020-02-17 09:02

Request is not always available in ASP.NET environment?

HttpContext and its properties Request/Response are initialized as soon as the server starts processing the page. So at any place you can execute c# code in your page life cycle you should be able to check the request url.

查看更多
登录 后发表回答