In this article I will explain with an example, how to return HttpResponse from Web Service (ASMX) in ASP.Net using C# and VB.Net.
The Web Service (ASMX) will return the HttpResponse along with HttpStatusCode in ASP.Net.
Web Service
Following is the Web Service (ASMX) for returning the HttpResponse.
This Web Service (ASMX) returns an OK response along with HttpStatusCode to the Client.
C#
using System;
using System.Web;
using System.Web.Services;
using System.Net;
///<summary>
/// Summary description for Service.
///</summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService {
public Service()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public void CheckStatus()
{
//Send OK Response to Client.
HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.OK;
HttpContext.Current.Response.Write("OK");
}
}
VB.Net
Imports System.Web
Imports System.Web.Services
Imports System.Net
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Public ClassService
Inherits System.Web.Services.WebService
<WebMethod()>
Public Sub CheckStatus()
'Send OK Response to Client.
HttpContext.Current.Response.StatusCode = CInt(HttpStatusCode.OK)
HttpContext.Current.Response.Write("OK")
End Sub
End Class
Adding Web Reference of Web Service
Next thing you need to do is add the Web Reference of the Web Service so that it can be used on the ASP.Net Web page.
HTML Markup
The HTML Markup consists of an HTML Button.
The Button has been assigned a jQuery Click event handler. When the Button is clicked, the Web Service (ASMX) is called using jQuery AJAX.
The HttpResponse along with HttpStatusCode are received in the Success event handler which are later displayed using JavaScript Alert Message Box.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<input type="button" id="btnCheck" value="Check Status"/>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$("body").on("click", "#btnCheck", function () {
$.ajax({
url: 'Service.asmx/CheckStatus',
type: 'POST',
data: {},
success: function (data, textStatus, http) {
alert("Data: " + data + "\nTextStatus: " + textStatus + "\nHttpStatus: " + http.status);
},
error: function (r) {
alert(r.responseText);
},
failure: function (r) {
alert(r.responseText);
}
});
});
</script>
</form>
</body>
</html>
Screenshot
Browser Compatibility
The above code has been tested in the following browsers.
* All browser logos displayed above are property of their respective owners.
Downloads