C# 将参数传递给httpget和httppost
我正在制作一个简单的Whois检查程序,以获取域名的Whois结果并将其显示在网站上 我正在使用MVC,我已经创建了checker类和视图,但是我不知道如何在我的控制器中准确配置httpget和httppost操作 这是whois类:C# 将参数传递给httpget和httppost,c#,asp.net-mvc,controller,http-get,C#,Asp.net Mvc,Controller,Http Get,我正在制作一个简单的Whois检查程序,以获取域名的Whois结果并将其显示在网站上 我正在使用MVC,我已经创建了checker类和视图,但是我不知道如何在我的控制器中准确配置httpget和httppost操作 这是whois类: using DATname.Models; using System; using System.Net; using System.Net.Sockets; using System.Text; public class ZapytanieWhois {
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")
}
欢迎任何帮助,因为我是MVC和C#的新手。您的
输入名称(name=“Domena”
)和控制器参数(CheckWhoIs(string Domena)
)必须匹配ModelBinder才能工作,即
<input type="text" height="10" width="30" name="domena"/>
另外,从设计的角度来看,如果要将ZapytanieWhois
类用作ViewModel
,那么您可能在ZapytanieWhois
类中做得太多了。相反,在我看来,只保留ViewModel
的简单属性,然后在控制器或控制器使用的助手方法中执行网络IO的繁重工作。您的表单
既没有动作
也没有方法
属性。我猜应该是这样。@ArtyomNeustroev说得好-默认操作返回到当前url-@ArtyomNeustroev-谢谢。在这种情况下,我可以删除标记并将其留给Html.BeginForm来处理数据,我猜?
<input type="text" height="10" width="30" name="domena"/>