简体   繁体   中英

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)

The Website(A) must pass an parameter that is a string and the Website(B) must return an parameter that is a byte[]

Another Example:

Website(A): www.xyz.com --- call a method and pass a string --> Website(B): www.abc.com

Website(B): www.abc.com --- Return a Byte[] ---> Website(A): www.xyz.com

I'm using 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.

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.

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. (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). This project will be integrated with MVC 6 in the next release.
  • WCF: You can use it to create SOAP and other kind of services.
  • 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

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. You may set up WCF service that expose SOAP protocol (for example), that will make possible to do kind of remote method calls.

I did it using Http request with Post: HTTP request with post

Here what I did:

WEBSITE 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM