0

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#.

Abbys
  • 137
  • 10
  • 1
    Your `form` has neither `action` nor `method` attributes. My guess, it should. – Artyom Neustroev Nov 08 '13 at 11:18
  • @ArtyomNeustroev Good point - the default action is back to the current url - http://stackoverflow.com/questions/16744191/how-does-html-beginform-works – StuartLC Nov 08 '13 at 11:32
  • @ArtyomNeustroev - thanks. In this case I can just delete the
    tags and leave it to Html.BeginForm to handle the data, I guess?
    – Abbys Nov 08 '13 at 11:53

1 Answers1

0

The case of your input name (name="Domena") and the controller parameter (CheckWhoIs(string domena)) must match for the ModelBinder to work, i.e.

<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.

StuartLC
  • 104,537
  • 17
  • 209
  • 285