简体   繁体   English

我如何在C#中调用另一个网站中的方法

[英]How i can call a method that is in another website in c#

I'm trying to call a method that is in another website. 我正在尝试调用另一个网站中的方法。

example: 例:

WebsiteMVC (A) ---> Call a method in the ---> WebsiteMVC (B) WebsiteMVC(A)--->在---> WebsiteMVC(B)中调用方法

The Website(A) must pass an parameter that is a string and the Website(B) must return an parameter that is a byte[] 网站(A)必须传递一个字符串参数,而网站(B)必须返回一个字节参数[]

Another Example: 另一个例子:

Website(A): www.xyz.com --- call a method and pass a string --> Website(B): www.abc.com 网站(A):www.xyz.com ---调用方法并传递字符串->网站(B):www.abc.com

Website(B): www.abc.com --- Return a Byte[] ---> Website(A): www.xyz.com 网站(B):www.abc.com ---返回一个字节[] --->网站(A):www.xyz.com

I'm using C#, MVC Razor. 我正在使用C#,MVC Razor。

How can I make this work? 我该如何进行这项工作?

First of all, it would be better if you can explain a little more why you want to call a method in another Website. 首先,最好能多解释一下为什么要在另一个网站中调用方法。 Is it because you provide services to other web sites and apps?. 是因为您向其他网站和应用程序提供服务吗?

You have two completely and separated MVC Projects/Sites so you can't call a method directly from Website A to Website B. 您有两个完全独立的MVC项目/站点,因此无法从网站A直接调用方法B。

Although you can call a Web page from A to B using MVC and return the array you want using a HTTP call and get the MVC controller from Site B to return your data, I wouldn't recommend it as there better ways to do it as using ASP.NET Web Api. 尽管您可以使用MVC从A到B调用网页,并使用HTTP调用返回想要的数组,并从站点B获取MVC控制器以返回数据,但我不建议这样做,因为有更好的方法使用ASP.NET Web Api。

Nowadays, there are many solutions for these cases: 如今,针对这些情况有许多解决方案:

  • ASP.NET Web Api: This is a library for .NET where you can create a Rest API using the HTTP protocol. ASP.NET Web Api:这是.NET的库,您可以在其中使用HTTP协议创建Rest API。 (I would suggest to try it as it easy to setup and use, also, in the open web most of the APIs I think use Rest APIs over http and using json). (我建议您尝试一下,因为它易于设置和使用,而且,在开放式网络中,我认为大多数API都可以通过http和json使用Rest API来使用)。 This project will be integrated with MVC 6 in the next release. 该项目将在下一版本中与MVC 6集成。
  • WCF: You can use it to create SOAP and other kind of services. WCF:您可以使用它来创建SOAP和其他类型的服务。
  • Sockets: this is another option that could not fit your needs as I believe you should have a good reason to go this way. 套接字:这是另一个无法满足您需求的选项,因为我认为您应该有充分的理由采用这种方式。
  • Other frameworks and libraries for web services Web服务的其他框架和库

That's not how it works. 那不是它的工作原理。 Two website are different applications, they cannot call methods of each other. 两个网站是不同的应用程序,它们不能互相调用方法。 It is not what websites do. 这不是网站做什么。

If you want your applications to communicate, you should use other solutions like web services for instance. 如果您希望应用程序进行通信,则应使用其他解决方案,例如Web服务。 You may set up WCF service that expose SOAP protocol (for example), that will make possible to do kind of remote method calls. 您可以设置公开SOAP协议的WCF服务(例如),从而可以进行某种远程方法调用。

I did it using Http request with Post: HTTP request with post 我使用Http请求和Post来完成: HTTP请求和post

Here what I did: 这是我做的:

WEBSITE A: 网站A:

            //request for method url in WEBSITE B
            var request = (HttpWebRequest)WebRequest.Create("http://someDomain/controllerName/methodName");

            var postData = "param1=" + _someString;
            postData += "&param2=" + _anotherString;

            var data = Encoding.ASCII.GetBytes(postData);

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;

            using (var stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }

            var response = (HttpWebResponse)request.GetResponse();

            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

WEBSITE B: 网站B:

           //Here I get the parameters:
           string _param1 = Request.Unvalidated["param1"];
           string _param2 = Request["param2"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何从 C# 中的另一个方法调用这个 function? - How can I call this function from another method in C#? 我可以在C#中的另一个方法调用事件上触发一个方法吗? - Can I trigger a method on another method call event in C#? 如何从另一个方法调用正在运行的方法的取消。 时间:2019-05-09 标签:c#.core 2.2 - How can I call the cancellation of a running method from another method. c# .core 2.2 我如何使用C#从另一个用户控件的用户控件调用方法? - How can i call method from usercontrol from another usercontrol using c#? C#-如何在同一个类的另一个方法中调用变量 - C# - How can I call a variable in a method from another in the same class 如何在C#中的另一个类之外调用非静态方法? - How can I call a non-static method out of another class in C#? C# 我如何学习在另一个 Class 中调用我的方法的 Class - C# How can I Learn Class that Call My Method in another Class 我可以在 C# 中另一个方法内的 foreach 循环中使用方法链调用方法吗? - Can I call a method using method chaining in a foreach loop that is inside another method in C#? c#如何在另一个void之后调用一个void? - c# How can I call a void after another void? 如何使用参数在 C# 中调用 Oracle 连接方法? - How can i call a Oracle connection method in C# with paramaters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM