问题
I am trying to pass a variable from an asp project (written in c#) to a desktop c# application. I know using Javascript you can use a JavaScriptSerializer, but is there an equivalent for asp?
回答1:
Depends on what you want to send back to the app or what is already being sent back by the server. i.e. let's say the result of a web page request were the "parameters" you speak of (you're not very clear, in the context of http and web communications). An ASP page can pass back the data to an app that requested data like this:
WebClient webClient = new WebClient();
var text = webClient.DownloadString("http://www.example.com/page.aspx");
... keep in mind, that's the code in the app. I assume that's what you're asking; because there's really only one way to data from an asp application.
If that's not what you're really asking, please be more detailed.
回答2:
but is there an equivalent for asp?
If you mean ASP.NET WebForms (it is worth being specific): your options include:
- Use MVC4 and use an ApiController to create a restful API endpoint.
- Use MVC and create a controller that returns HTML/XML/JSON depending on the accept header)
- Add an IHTTPHandler mapped to a specific file extension to return the data. (This could use the accept header to return different formats).
- Add an WCF endpoint.
I would tend to go for the first (allows me to learn ApiController with a simple example), but the second and third are both easy enough.
The final option gives you the greatest flexibility especially if SOAP/WS-* style is something you need) but the greatest learning curve.
For the first three the client is WebClient (or perhaps HttpWebRequest) with a known (or dynamically determined) URL. In the latter case WCF.
NB. A single web application (in IIS terms) can include a mixture of WebForms and MVC.
来源:https://stackoverflow.com/questions/11594259/how-can-i-pass-a-variable-from-a-asp-website-to-a-desktop-application