[英]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: 如今,针对这些情况有许多解决方案:
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 += "¶m2=" + _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.