0

我正在制作一个简单的 Whois 检查器来获取域名的 whois 结果并将其显示在网站上。

我正在使用 MVC,并且创建了检查器类和视图,但是我不知道如何在控制器中准确配置 httpget 和 httppost 操作。

这是 whois 类:

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;
    }
}

这是我的观点:

@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")
}

我需要的是控制器动作,我尝试过这样的事情,但没有任何反应:

        [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);
    }

任何帮助都会受到欢迎,因为我是 MVC 和 C# 的新手。

4

1 回答 1

0

您的input姓名 ( name="Domena") 和控制器参数 ( CheckWhoIs(string domena)) 的大小写必须匹配才能使 ModelBinder 工作,即

<input type="text" height="10" width="30" name="domena"/>

此外,从设计的角度来看,ZapytanieWhois如果要将其用作ViewModel. 相反,IMOViewModel只保留简单的属性,然后在控制器中或在控制器使用的辅助方法中执行繁重的网络 IO。

于 2013-11-08T11:17:08.263 回答