Tugas telah selesai dalam beberapa hari terakhir, dan tidak ada yang penting. Saya meluangkan waktu untuk belajar tentang layanan web. Rasanya cukup menarik dan tidak terlalu sulit.
Pertama, buat situs web ASP.NET dengan VS2008
Kedua, klik kanan proyek -> tambahkan item baru -> layanan web seperti yang ditunjukkan pada gambar di bawah ini:
Dua file, WebService.cs dan WebService.asmx, akan dihasilkan
Tambahkan kode di WebService.cs:
Salinan kode adalah sebagai berikut:
menggunakan sistem;
menggunakan system.collections.generic;
menggunakan System.linq;
menggunakan System.web;
menggunakan System.Web.Services;
menggunakan System.Web.Script.Services;
/// <summary>
/// Ringkasan Deskripsi Webservice
/// </summary>
[WebService (namespace = "http://tempuri.org/")]]
[WebServiceBinding (conformSto = wsiprofiles.basicprofile1_1)]]
// Tambahkan untuk menambahkan kode berikut //
[Scriptservice]
// Untuk mengizinkan memanggil layanan web ini dari skrip menggunakan ASP.Net Ajax, Uncomment the Downline.
// [System.Web.Script.Services.ScriptService]
WebService kelas publik: System.web.services.webservice
{
Service web publik ()
{
// Jika menggunakan komponen yang dirancang, silakan buka komentar baris berikut
// initializeComponent ();
}
[WebMethod]
string publik helloworld ()
{
Kembalikan "Halo Dunia";
}
[WebMethod]
int getsum publik (int a, int b)
{
int sum = a + b;
jumlah pengembalian;
}
}
Halaman default.aspx
Salinan kode adalah sebagai berikut:
< %@ Page language = "c#" autoeventwireup = "true" codefile = "default.aspx.cs" mewariskan = "_ default" %>
<! Doctype html public "-// w3c // dtd xhtml 1.0 transisi // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head runat = "server">
<title> </title>
</head>
<bahasa skrip = "javascript">
Metode Fungsi (OBJ)
{
document.geteLementById ("txtsum"). value = obj;
}
fungsi hello ()
{
WebService.Helloworld (backmethod);
}
fungsi getsum ()
{
var a, b;
a = document.geteLementById ("txta"). value;
b = document.geteLementById ("txtb"). value;
mencoba
{
Webservice.getsum (a, b, metode);
}
Catch (err)
{
peringatan (err.description);
}
}
</script>
<body>
<Form id = "Form1" runat = "server">
<div>
<asp: scriptManager id = "scriptManager1" runat = "server">
<service>
<asp: serviceereference inlinescript = "true" path = "webservice.asmx" />
</Services>
</sp: ScriptManager>
<input type = "tombol" id = "bthello" value = "hello" onclick = "hello ();" /> <br />
<input type = "text" id = "txta" value = "" />+
<input type = "text" id = "txtb" value = "" /> =
<input type = "text" id = "txtsum" value = "" />
<input type = "tombol" id = "btsum" value = "sum" onclick = "getsum ();" /> <br />
</div>
</form>
</body>
</html>
Melalui metode di atas, Anda dapat dengan mudah memanggil metode di WebService, dan set hasil dataset juga dapat dikembalikan di WebService.
Kami harus terus belajar layanan web nanti.
Jika Anda memiliki materi pembelajaran layanan web yang bagus atau situs web, silakan bagikan sehingga Anda dapat belajar dan berkomunikasi bersama.