在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);
这个应该是可以的。我测了
c# – Asp.Net Core MVC中Request.IsAjaxRequest()在哪里?
大多数事情看起来真的很有希望,但我似乎找不到Request.IsAjaxRequest() – 一个在旧的MVC项目中经常使用的功能。
有没有更好的方法来做到这一点 – 这使得他们删除这种方法 – 或者是“隐藏”在别的地方?
感谢任何建议,在哪里找到它或做什么改为!
搜索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"
- 1. asp.net-mvc – ASP.NET MVC RC中的Html.Image在哪里?
- 2. asp.net-mvc – 在哪里得到的Microsoft.Web.Mvc.dll
- 3. asp.net-mvc – 在哪里放置AutoMapper.CreateMaps?
- 4. asp.net-mvc-4 – ASP.NET MVC 4.0 RTM的符号在哪里?
- 5. asp.net-mvc – ASP.NET MVC – 在哪里抛出异常?
- 6. asp.net-mvc – asp.net mvc – 在哪里存储userid – integer?
- 7. asp.net – System.Web.Security.MembershipProvider在哪里?
- 8. asp.net-core-mvc – ASP.NET Core MVC控制器在单独的程序集中
- 9. C# – IoC和ASP.NET MVC,它在哪里开始?
- 10. asp.net-mvc – web.config在哪里用于MVC应用程序?
- 更多相关文章...
以上是关于在asp.net中使用"Request.UserHostAddress"为何获取的ip地址是::1?的主要内容,如果未能解决你的问题,请参考以下文章
ASP.NET(C#)如何使用期Request获取通过网址传递的参数?
asp.net 后台 Request.Params时遇到前台发送的中文字符 , 得到乱码怎么解决?