在asp.net中使用"Request.UserHostAddress"为何获取的ip地址是::1?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在asp.net中使用"Request.UserHostAddress"为何获取的ip地址是::1?相关的知识,希望对你有一定的参考价值。

根据您的描述与贴图,您应该是通过localhost或通过127.0.0.1来进行访问了.
登录IP:::1
指的就是当前计算机.
这是一个IP地址类似于"回环地址"而已.

记得,如果您是学问的.那么这个"回环地址"已经要引起来,只是类似,因为在不考虑服务器集群等等情况下,目前你能遇到的"回环"就是这样产生的.

其他的情况有很多,这个需要您自行查找资料了,这个知识点内容不少.
参考技术A Request.UserHostAddress.ToString(); 这样就可以了追问

?这是不行的。

追答

只能获取本机的么?
protected void Button1_Click(object sender, EventArgs e)

string result = String.Empty;
//服务端获取IP地址
result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (null == result || result == String.Empty)

result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

if (null == result || result == String.Empty)

result = HttpContext.Current.Request.UserHostAddress;

if (null == result || result == String.Empty)

return;

Response.Write(result);

这个应该是可以的。我测了

参考技术B 因为你是本地访问所以是这样的,远程访问即可获取真实IP地址

c# – Asp.Net Core MVC中Request.IsAjaxRequest()在哪里?

要了解有关新的令人兴奋的Asp.Net-5框架的更多信息,我正在使用最新发布的Visual Studio 2015 CTP-6来构建一个Web应用程序。

 

大多数事情看起来真的很有希望,但我似乎找不到Request.IsAjaxRequest() – 一个在旧的MVC项目中经常使用的功能。

有没有更好的方法来做到这一点 – 这使得他们删除这种方法 – 或者是“隐藏”在别的地方?

感谢任何建议,在哪里找到它或做什么改为!

 
我有点困惑,因为标题提到了MVC 5。

 

搜索Ajax in the MVC6 github repo doesn’t give any relevant results,但您可以自己添加扩展。从MVC5项目中进行的解压缩代码很简单:

 

/// <summary>
/// Determines whether the specified HTTP request is an AJAX request.
/// </summary>
/// 
/// <returns>
/// true if the specified HTTP request is an AJAX request; otherwise, false.
/// </returns>
/// <param name="request">The HTTP request.</param><exception cref="T:System.ArgumentNullException">The <paramref name="request"/> parameter is null (Nothing in Visual Basic).</exception>
public static bool IsAjaxRequest(this HttpRequestBase request)

  if (request == null)
    throw new ArgumentNullException(nameof(request));
  if (request["X-Requested-With"] == "XMLHttpRequest")
    return true;
  if (request.Headers != null)
    return request.Headers["X-Requested-With"] == "XMLHttpRequest";
  return false;

由于MVC6 Controller似乎使用Microsoft.AspNet.Http.HttpRequest,您必须通过对MVC5版本引入少量调整来检查request.Headers collection是否适合标题:

 

/// <summary>
/// Determines whether the specified HTTP request is an AJAX request.
/// </summary>
/// 
/// <returns>
/// true if the specified HTTP request is an AJAX request; otherwise, false.
/// </returns>
/// <param name="request">The HTTP request.</param><exception cref="T:System.ArgumentNullException">The <paramref name="request"/> parameter is null (Nothing in Visual Basic).</exception>
public static bool IsAjaxRequest(this HttpRequest request)

  if (request == null)
    throw new ArgumentNullException("request");

  if (request.Headers != null)
    return request.Headers["X-Requested-With"] == "XMLHttpRequest";
  return false;

或直接:

 

var isAjax = request.Headers["X-Requested-With"] == "XMLHttpRequest"
 

以上是关于在asp.net中使用"Request.UserHostAddress"为何获取的ip地址是::1?的主要内容,如果未能解决你的问题,请参考以下文章

ASP.NET(C#)如何使用期Request获取通过网址传递的参数?

Asp.net中怎么接收post方式提交过来的数据?

asp.net 怎么使用post传值

asp.net 后台 Request.Params时遇到前台发送的中文字符 , 得到乱码怎么解决?

asp.net 网页套用母版后,其后台代码用 Request.Files 无法获取值,郁闷!

ASP.NET如何判断Cookie为空?