I'm making a simple Whois checker to get the whois result for a domain name and display it on the website.
I'm using MVC and I've created the checker class and a View, however I don't know how exactly to configure httpget and httppost actions in my controller.
This is the whois class :
using DATname.Models;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class ZapytanieWhois
{
public string NazwaDomeny { get; set; }
public string RodzajTLD { get; set; }
public string SerwerWhois { get; set; }
public string Odpowiedz { get; set; }
public void SprawdzanieTLD(string Domena)
{
Domena = NazwaDomeny;
if (Domena.Contains("http://"))
{
Domena = Domena.Replace("http://", "");
}
if (Domena.Contains("www."))
{
Domena = Domena.Substring(4);
}
else
{
if (Domena.IndexOf('.') != -1)
{
int kropka = Domena.IndexOf('.');
string TLDzKropka = Domena.Substring(kropka);
string TLD = TLDzKropka.Replace(".", "");
RodzajTLD = TLD;
this.SerwerWhois = this.UstalanieSerweraNaPodstawieTLD(TLD);
// Connect to the whois server
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(this.SerwerWhois, 43);
NetworkStream networkStream = tcpClient.GetStream();
// Send the domain name to the whois server
byte[] buffer = ASCIIEncoding.ASCII.GetBytes(Domena + "\r\n");
networkStream.Write(buffer, 0, buffer.Length);
// Read back the results
buffer = new byte[8192];
int i = networkStream.Read(buffer, 0, buffer.Length);
while (i > 0)
{
i = networkStream.Read(buffer, 0, buffer.Length);
Odpowiedz += ASCIIEncoding.ASCII.GetString(buffer); ;
}
networkStream.Close();
tcpClient.Close();
}
else
{
Odpowiedz = "Prosze wpisać poprawną domenę.";
}
}
}
private string UstalanieSerweraNaPodstawieTLD(string TLD)
{
DatnameContext db = new DatnameContext();
string serwer = db.TLDs.Find(TLD).Whois_Server;
return serwer;
}
}
This is my view :
@model ZapytanieWhois
@{
ViewBag.Title = "Dane szczegółowe";
}
@section featured {
<section class="featured">
<div class="content-wrapper">
<hgroup class="title">
<h2>WHOIS.</h2>
</hgroup>
</div>
</section>
}
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<form>
Wpisz nazwę domeny : <input type="text" height="10" width="30" name="Domena" id="Domena"/> | <input type="submit" value="Sprawdź Who-Is" />
</form>
<div class="display-label">
<strong>Odpowiedź serwera: </strong>
@Html.DisplayFor(model=> model.Odpowiedz)
</div>
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
What I need is the controller action, I'ved tried with something like this, but nothing happens :
[HttpGet]
public ActionResult CheckWhoIs(string domena)
{
var model = new ZapytanieWhois();
model.NazwaDomeny = domena;
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CheckWhoIs(ZapytanieWhois zapytanie)
{
return View(zapytanie);
}
Any help would be welcomed as I'm new to MVC and C#.
The case of your input
name ( name="Domena"
) and the controller parameter ( CheckWhoIs(string domena)
) must match for the ModelBinder to work, ie
<input type="text" height="10" width="30" name="domena"/>
Also, from a design point of view, you might be doing too much in your ZapytanieWhois
class if it is to be used as a ViewModel
. IMO, instead, keep your ViewModel
just the simple properties, and then do the heavy lifting of network IO in the controller, or in a helper method used by the controller.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.