asp.net(C#) 如何截取ip地址

有没有人在啊,想请说下,asp.net(C#) 如何截取ip地址?

例如,获得的 ip 地址为 202.34.66.212
我想在页面显示 202.34.66.*

asp.net(C#) 应该怎么做呢?
最新回答
晚街听风

2024-04-15 04:45:26

方法很多,就是处理字符串的问题
原理:首先查找字符串最后一个"."的位置,然后再删除最后一个点后面的字符再加个"*"
举例:
string ip = Request.UserHostAddress;
ip=ip.Remove(ip.LastIndexOf('.')+1)+"*";
Response.Write(ip);

以上本机输出结果为127.0.0.*

当然你要不用Remove而用SubString截取最后个"."前的字符串加*也是可以的。
甚至你可以写个for语句再来一个一个字符的读,发现有三个"."后就不读了,然后+"*",
甚至你还可以从字符后面读起碰到一个"."就跳出...
提供些思路自己领悟吧,C#处理字符串很强的
空城已无她

2024-04-15 12:40:06

这不就是一个字符的操作嘛
...
string strIP = "202.34.66.212";
int position = strIP.LastIndexOf(".");
string result = strIP.SubString(0, position + 1) + @".*";
...
大概就是这个思路,呵呵,你可以再修改下下 :)

推荐一些资料你,参考一下:

ASP.NET1.x/2.0电子资料汇总:

O'Reilly Silverlight 1.1简介
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=163145

Wrox出版 Silverlight 1.0 (彩页染色代码、全面解析)
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=165953

Silverlight 1.0 Development with JavaScript
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=163268

Sams出版 Silverlight 1.0 Unleashed
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=162975

Silverlight and ASP.NET Revealed
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=174174

O'Reilly Essential Silverlight
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=163266

XAML简明教程 CHM+PDF版
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=163430

Pro ASP.NET 3.5 in C# 2008
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=166640

Beginning ASP.NET 3.5 in VB 2008 从入门到精通
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=166774

Wrox ASP.NET 2.0 MVP Hacks and Tips
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=163467

Professional ASP.NET.2.0 Design
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=160236

Professional Web Parts and Custom Controls with ASP.NET 2.0
http://bbs.topsage.com/dispbbs.asp?BoardID=123&ID=162787

ASP.NET2.0入门经典
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=146151

ASP.NET 2.0 技术内幕 微软技术丛书 清华大学出版
http://bbs.topsage.com/dispbbs_123_171293_1.html

Wrox ASP.NET 2.0 Visual Web Developer 2005 Express Edition Starter
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=145208

Beginning ASP.NET 2.0 in C# 2005 From Novice to Professional
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=145396

Wrox Professional ASP.NET 2.0
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=145202

Wrox Professional ASP.NET 2.0 XML
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=145197

Wrox Professional ASP.NET 2.0 Security Membership and Role Management
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=145198

Wrox Beginning ASP.NET 2.0 and Databases
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=147078

Pro ASP.NET 2.0 in VB 2005
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=173790

ASP.NET开发人员手册
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=145391

ASP.NET 2.0网络编程入门到精通
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=145342

ASP.NET Web应用程序开发新思维
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=145302

ASP.NET 2.0高级应用程序设计专家教程
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=146379

ASP.NET XML高级编程 C#编程篇
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=148800

ASP.NET程序开发 C#篇
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=159285

ASP.NET XML深入编程技术
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=148795

ASP.NET 2.0 Cookbook
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=151984

ASP.NET 2.0 Everyday Apps for Dummies
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=158103

Pro ASP.NET for SQL Server
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=167279

ASP.NET 从入门到精通
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=159331

Wrox Beginning ASP.NET 1.1 with Visual C#.NET 2003
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=160240

ASP.NET 2.0 揭秘
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=160249

Build Your Own ASP.NET 2.0 Web Site Using C# and VB
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=163143

开发Microsoft ASP.NET 2.0网络应用程序
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=160473

Pro ASP.NET 2.0 Website Programming
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=174179

开发ASP.NET 2.0 核心参考
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=160474

Building Websites with VB.NET and DotNetNuke 4
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=160639

Wrox出版 Professional DotNetNuke 4.0
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=166008

Professional DotNetNuke ASP.NET Portals
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=163399


Ajax,ASP.NET Ajax电子资料汇总:

Ajax基础教程
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=144263

Ajax宝典
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=148421

Wrox Beginning Ajax
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=146082

Ajax in Practice
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=145551

Ajax模式最佳实践教程
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=146080

Wrox Professional Rich Internet Applications AJAX and Beyond
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=160608

O'Reilly Ajax on Java
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=160600

Practical JavaScript DOM Scripting and Ajax Projects
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=150277

Creating Web Pages with Asynchronous Javascript and XML
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=162038

O'Reilly Securing Ajax Applications
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=162009

Advanced Ajax Architecture and Best Practices
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=163068

Beginning ASP.NET 2.0 AJAX
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=151273

Introducing Microsoft ASP.NET AJAX
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=144519

Wrox Professional ASP.NET 2.0 AJAX
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=146172

O'Reilly Programming ASP.NET Ajax
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=172105

ASP.NET AJAX Programmer's Reference
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=163394