[英]How do I make an async call to an MVC Controller from Razor Pages rendered screen
[英]how do i call webservice from MVC3 Razor Controller?
在我的项目中,我需要从控制器调用Web服务。 我已经完成了以下工作,但它确实有效。
将Web服务的Web引用添加到项目中。
按如下方式呼叫服务:
Service Wservice=new Service(); Wservice.loginCompleted+=new Wservice_login_Completed; WService.login_Async("username","Password");
注意:每当我调用此服务时,它都会抛出一个错误,“此时无法启动异步操作。异步操作只能在异步处理程序或模块中启动,或者在页面生命周期中的某些事件中启动。如果发生此异常执行页面,确保页面标记为<%@ Page Async =“true”%>。“
为了克服这个问题我使用
[Httppost]
public ActionResult login(logmodel model)
{
Task.Factory.StartNew(() =>
{
Wservice.loginCompleted+=new Wservice_login_Completed;
WService.login_Async("username","Password");
});
if(finalresult==true)
{
*** return View();
}
}
void Wservice_login_completed()
{
Here i got the output.
}
但调用Wservice_login_completed()函数是在返回View ***之后,所以我没有得到结果。 我如何实现“从Controller调用web服务”..任何想法?
最后,我成功地从MVC Controller调用了web服务。
注意: 添加ServiceReference而不是WebReference并避免
“Task.Factory.StartNew(()=>);” 处理。
[Httppost]
public ActionResult login(logmodel model)
{
Wservice.ServiceSoapClient _host = new Wservice.ServiceSoapClient("ServiceSoap");
var result_out = _host.login(uname, pwd, "test1", "test2", "test3", "test4");
}
这里“ServiceSoap”是我们服务的端点。您可以在app.confiq或web.config文件中显示端点。 快乐的编码......!
得到以下NuGet:
microsoft http client (id = Microsoft.Net.Http)
创建Web API控制器(webapi_Controller_Name)
Post函数应类似于以下函数
将此功能放在Web Api Controller中
[HttpPost] public void PostForm(objUser ws_Obj) { // put you code here }
从常规Controller调用Web服务,如下所示。 这是一个异步调用,Web服务将立即返回。
//call the web service, Asynch HttpClient client = new HttpClient(); client.BaseAddress = new Uri("52323/"); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); client.PostAsJsonAsync("//52323/api/webapi_Controller_Name/PostForm", objContact);
首先,通过右键单击解决方案资源管理器中的项目名称来创建服务引用,然后将鼠标悬停在“添加”选项上,然后单击“服务引用...”
其次,将您的Web服务地址粘贴到“添加服务引用”页面的“地址”字段中,确保将“?wsdl”添加到您的Web服务地址的末尾,否则它将无法工作,然后按“转到” ”。 您将看到Web服务显示在“服务”区域中。 单击该服务以查看将显示在“操作”部分中的可用服务。 如果需要,重命名服务,然后按确定以创建服务。
最后,将以下代码放入MVC Controller中。 将代码放在Get或Post控制器中,没关系。
// Declare the Request object.
ServiceReference1.GetSurveyRequest myGSRq = new ServiceReference1.GetSurveyRequest();
// You can set the webservice parameters here like...
myGSRq.Address = getUserData[0].address;
myGSRq.City = getUserData[0].city;
myGSRq.State = getUserData[0].state;
// Now declare the Response object.
ServiceReference1.GetSurveyResponse myGSR = new ServiceReference1.GetSurveyResponse();
//And then use the following to process the request and response.
ServiceReference1.EMPortTypeClient emptc = new ServiceReference1.EMPortTypeClient();
myGSR = emptc.GetSurvey(myGSRq);
// In this example the response Object comes back with a URL used to redirect the user
//after the webservice has been processed.
if (myGSR.Url != null)
Response.Redirect(myGSR.Url.ToString());
else
Response.Write("Error");
return null;
很简单,希望这有帮助!
如果您要创建新服务并可以选择使用Web服务或Web API,我建议使用Web API。 使用ASP.NET Web API构建RESTful API
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.