Reputation: 341
I am developing an ASP.NET project for our intranet which retrieves client's MachineName and IPAddress and I am using the following code.
ajax/default.aspx.vb
Public Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Request.QueryString.Count > 0 Then
Select Case Request.QueryString("eval")
Case Else
Dim type As Type = type.GetType("ajax_default")
Dim method As MethodInfo = type.GetMethod("zzfxn_" & Request.QueryString("eval").ToString, BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance)
If method IsNot Nothing Then
method.Invoke(Me, New Object() {Request})
End If
End Select
End If
End Sub
Public Sub zzfxn_zGetClientInfo(ByVal r As System.Web.HttpRequest)
Response.Write("Client IP Address: " & Request.UserHostName & "|" & Request.UserHostAddress & "<br />")
Response.Write("Client Machine Name: " & System.Net.Dns.GetHostEntry(Request.UserHostName).HostName.Split(".")(0))
End Sub
test/Default.aspx
<%@ Page Language="VB" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">.hid{display: none;}</style>
</head>
<body>
<form id="form1" name="form1" runat="server">
<div>
<p>My client info is: <%= InvokeAjax("?eval=zGetClientInfo")%></p>
</div>
</form>
<script type="text/VB" runat="server">
Protected Function InvokeAjax(ByVal params As String) As String
Dim wc As New System.Net.WebClient
wc.Headers.Add("user-agent", Request.UserAgent)
Dim ru As String = Request.Url.ToString.Remove(Request.Url.ToString.IndexOf("/test")) & "/ajax/default.aspx"
Return wc.DownloadString(ru & params).ToString
End Function
</script>
</body>
</html>
I deployed the project to the test-server Windows Server 2000, ASP.NET 4 and I got the following results.
Running/viewing ajax/Default.aspx?eval=zGetClientInfo results to:
Client IP Address: 10.20.10.5|10.20.10.5
Client Machine Name: devctr
Running/viewing test/Default.aspx results to:
My client info is: Client IP Address: 10.20.10.60|10.20.10.60
Client Machine Name: test-server
10.20.10.5 or devctr is my machine so viewing directly to ajax/default.aspx returns the exact/true content, while 10.20.10.60 or test-server is my host/server.
Can someone please explain to me why they differ? And how can I achieve the results of ajax/defaut.aspx to be exactly the same for test/Default.aspx?
Upvotes: 0
Views: 95
Reputation: 337
In the test/Default.aspx example, you are creating a web request that is originating on the server, hence the "runat='server'" parameter. If you are trying to create an ajax request that originates from the client, you will need to use a client side script like javascript. You could use jQuery to do this by doing something like:
$.ajax({
url: "/?eval=zGetClientInfo"
}).done(function(data) {
//Set your html here
});
If you are really going to do this, however, I would consider looking into ASP.NET Web API. Your solution will be much more robust if you return your results in some sort of JSON or XML format, which web API will do.
Upvotes: 3