如何从action方法调用web方法? [英] How to call a web method from action method?

查看:116
本文介绍了如何从action方法调用web方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从控制器调用wen方法

这是我的代码。

How can I call a wen method from controller
Here is my code.

[WebMethod]
      public IEnumerable<CitySearchResult>  SearchFromHomeBus(int fromid, int toid)
      {
         // DbDataReader sqlreader;
          SqlParameter []para=new SqlParameter[2];
          para[0]=new SqlParameter("@city1",fromid);
          para[1]=new SqlParameter("@city2",toid);
          using (cab_4_21Entities1 d = new cab_4_21Entities1())

          {
              ObjectResult<CitySearchResult> search =d.ExecuteStoreQuery<CitySearchResult>("usp_SearchcabResult @city1, @city2", para);
              //List<CitySearchResult> search = (d.ExecuteStoreQuery<CitySearchResult>("usp_SearchcabResult", para)).ToList<CitySearchResult>();
              List<CitySearchResult> searchResult=search.ToList<CitySearchResult>();

              return searchResult;
          }
      }



请建议我。


Please suggest me.

推荐答案

如果您将Web服务作为单独的端点(在消费的MVC应用程序之外)托管,那么您需要在操作合同(假设其WCF)或当前的WebMethod上提供WebGet或WebInvoke属性。 />


然后通过浏览到服务添加ServiceReference(如果是WCF)或WebReference(如果是ASMX)。 VS将创建相应的代理并为您提供一个可以通过其进行调用的对象。



但是,如果该服务仅由一个应用程序使用,那么它不是定义的服务;它只是一个执行给定功能的类库。在这种情况下,它应该像通常的库引用一样被消费。
If you are hosting the web service as a separate endpoint (outside the consuming MVC app), then you need to provide the WebGet or WebInvoke attributes on the operation contracts (assuming its WCF) or the current WebMethod as you have put it.

Then add a ServiceReference (if WCF) or WebReference (if ASMX) by browsing to the service. VS will create corresponding proxies and give you an object through which you can make the calls.

However, if the service is meant to be consumed by only ONE application, then its not a service by definition; its just a class library doing a given function. In such a case, it should be just taken in like that with the usual library reference and consumed.


这篇关于如何从action方法调用web方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆