TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Ankush Band
NA
195
7.5k
self hosting web api
Aug 29 2017 9:46 AM
How to access self hosting web api in windows application from another system through ajax call?
when I access self hosted webapi method of windos application from client application through ajax call from another system then it can't access. I explain my self hosted application as follows:
I create windows application and it derive from apicontroller it host self in system A with using System.Web.Http.SelfHost dll. following is my code :
In Program.cs :
using
System;
using
System.Web.Http;
using
System.Web.Http.SelfHost;
using
System.Windows.Forms;
namespace
SelfHost
{
static
class
Program
{
[STAThread]
static
void
Main(
string
[] args)
{
var config =
new
HttpSelfHostConfiguration(
"http://182.150.1.1:8080/api"
);
config.Routes.MapHttpRoute(
name:
"API"
,
routeTemplate:
"{controller}/{action}/{id}"
,
defaults:
new
{ id = RouteParameter.Optional }
);
using
(HttpSelfHostServer server =
new
HttpSelfHostServer(config))
{
server.OpenAsync().Wait();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(
false
);
Application.Run(
new
MainForm());
}
}
}
}
In ProductsController.cs :
using
System.Web.Http;
namespace
SelfHost
{
public
class
ProductsController : ApiController
{
MainForm mainForm =
new
MainForm();
[HttpPost]
public
IHttpActionResult GetResponse(
string
message)
{
mainForm.TextBoxRequestMsg = message;
return
Json(
"Response from server successfully with message "
+ message);
}
}
}
In MainForm.cs :
using
System;
using
System.Windows.Forms;
namespace
SelfHost
{
public
partial
class
MainForm : Form
{
private
string
_TextBoxRequestMsg;
public
string
TextBoxRequestMsg
{
get
{
return
txtReqMsg.Text; }
set
{
_TextBoxRequestMsg = value;
MessageBox.Show( _TextBoxRequestMsg);
}
}
public
MainForm()
{
InitializeComponent();
}
}
}
Following is client application this application and is hosted on another system using this application we can call of self hosted webapi method GetResponse() from another system using jquery Ajax call. Following is my client code:
var
message =
"test request"
;
$.ajax({
url:
'http://182.150.1.1:8080/api/Products/GetResponse?message='
+ message,
cache:
false
,
type:
'POST'
,
dataType:
'json'
,
contentType:
'application/json; charset=utf-8'
,
async:
false
,
processData:
false
,
success:
function
(data) {
alert(
"Successfully get response."
);
},
error:
function
(err) {
alert(
"Call to web api failed."
);
}
});
Also I stop firewall of both system also I enable inbound rule of "World Wide Web Services (HTTPS Traffic-In)"
tell me any possible solutions.
Reply
Answers (
2
)
Create the loop in javascript
html with a warning