Running DNSClient.Net on a C# Razor Page

442 Views Asked by At

I am new to coding, but do okay at reusing code. I just can't find a good example of running code on a razor page and displaying the info.

Here is the code I want to run on a razor webpage:

enter code here

@page "/revdns" @inject DnsClient

Reverse DNS Checker

This where we will display the DNS records..

@record

@ip

@code {

string lookup = new LookupClient();
string result = await lookup.QueryAsync("google.com", QueryType.A);

string record = result.Answers.ARecords().FirstOrDefault();
string ip = record?.Address;

}

Hopefully someone can help

I have now tweaked the code

I am new to coding, hoping you can help. I am getting the following errors.

Error CS0103 The name 'QueryType' does not exist in the current context

Error CS0103 The name 'record' does not exist in the current context

@page "/dnsquery"

DNS Query

This where we will display the DNS records..

Record: @Record

IP Address: @Ip

@code {

protected override async Task OnInitializedAsync()
{
    
    var lookup = new DnsClient.LookupClient();
    var result = await DnsClient.LookupClient.QueryAsync("google.com", QueryType.A);
    Record = result.Answers.ARecords().FirstOrDefault();
    Ip = record?.Address;
}

string Record = "Loading...";
string Ip = "Loading...";

}

1

There are 1 best solutions below

6
On

You can't use the 'async' keyword in a field declaration - you probably want to do the actual lookup in one of the lifecycle methods (example using OnInitialized for simplicity - look into OnAfterRender too)

Note, not addressing what LookupClient is or why you are injecting DnsClient...

@page "/revdns" 
@inject DnsClient

Reverse DNS Checker
This where we will display the DNS records..

@Record

@Ip

@code {

     protected override async Task OnInitializedAsync(){
        string lookup = new LookupClient();
        string result = await lookup.QueryAsync("google.com", QueryType.A);
        Record = result.Answers.ARecords().FirstOrDefault();
        Ip = record?.Address;
     }

    string Record = "Loading...";
    string Ip = "Loading...";
}