call a page within subdomain using ajax - c#

I need to read a page content on my sub-domain using ajax get.
I thought of creating some script on my sub-domain and calling that script on my domain. however I don't know how to do it.
Is it possible to do such a thing? how?
EDIT:
I created a page in my domain that uses DownloadString and downloads that page and write down the result. any better ideas?

If you wish to support cross-domain AJAX requests, you will need to create a cross origin resource sharing handler (CORS). I don't have an example specific to asp.net, but if you are using MVC this is fairly simply to achieve (and would be fundamentally the same for WebForms).
A Web API example may be found here to get you started:
CorsHandler.cs
Also, if you need to support IE 7, 8 or 9 you would want to look in to a jQuery extension lib such as jQuery-ajaxTransport-XDomainRequest
EDIT
If you would like to look at a full example (again Web API unfortunately), here is a MSDN article with a full code example Implementing CORS support in ASP.NET Web APIs

You can also sometimes manipulate document.domain in JS. So if you're serving pages from www.site.com, and your Ajax services are hosted at app.site.com, set your document.domain to site.com.

Related

enable download on html from my asp page

I want to be able to download the html contents of my page from a client after receiving a post request (XMLHttpRequest and doing some processing. Is there a setting I must enable for this to be possible? I've tried enabling COORS via
Response.AddHeader("Access-Control-Allow-Origin", "*");
with no success. Has anyone found a solution to this problem?
Update: I am using ASP.NET Web Forms (with NET 4.5)
Well, I think the CORS stuff is for cross origin scripting - to enable JavaScript calls to a domain different than the original request. As far as getting the HTML contents, I believe you mean to get the response of the web server as it is posted back to the client. Not knowing what technology you have used (i.e., MVC, ASPX, etc.) you may want to look into interceptors. They differ depending on the technology. For ASPX, check out....
http://www.codeproject.com/Articles/30907/The-Two-Interceptors-HttpModule-and-HttpHandlers

How can I pass a variable from a ASP website to a desktop application?

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?
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.
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.

OAuth User-Agent Flow with C# Desktop App

I'm currently attempting to use the OAuth 2.0 User-Agent Flow with a clientside C# application, and I'm running into some confusion relating to the redirect URI.
Because I'm working with a clientside application, I cannot supply a standard redirect URL to a web server. However, according to the people I'm trying to authenticate with (Salesforce, in this instance), the User-Agent Flow is the correct one to use for a clientside application.
My question is, what can I do to catch the access token in this situation? Apparently I can create a "local resource accessible to the client," but I'm unfamiliar with the mechanics behind this, and I can't find any resources on the topic (partly because I don't know what to look for).
Any pointers as to where I should start looking would be greatly appreciated.
Edit: Some more digging has revealed the following stackoverflow question:
How do I develop against OAuth locally?
I'm doing some more investigating with what they suggested, but any other suggestions would be great as well.
Edit: Some more searching revealed this article:
http://sarangasl.blogspot.com/2010/09/create-simple-web-service-in-visual.html
Still feels like I'm poking around in the dark without an understanding of the larger picture, but I believe I need to set up a local web service using localhost and point my redirect URI there. I'll then use my web service to unwrap the response from the OAuth server and have my application respond appropriately. More updates to come.
Ooookay. So from what I've been able to gather, I need to set up a local web service to supply as the callback for OAuth. I need to listen on said web service myself and catch the callback to pass it to my app. However, the default ASP.NET web service provided by VS2010 does not support URL parameters, just API calls, so I apparently need to use the WCF Rest starter kit instead.
I am completely foreign to all of this, so any tips would be a godsend at this point. In general, I'm thinking I set up a local WCF Rest service, supply that local URI to OAuth as the callback, and then catch the callback URL using the Rest service. Then I parse the URL and extract the access token. At this point, does my app request the access token, or can my web service "give" the token to my app? I.e., where should the locus of control be?
Figured out a clever way to work around this. Instead of setting up a service to listen for OAuth's redirect URL, I embedded a WebBrowser control inside my Windows form.
I pointed this embedded WebBrowser to the authentication URL and let the user log in and authenticate with Salesforce and grant permissions to my app. Then, I let Salesforce redirect my embedded browser to a dummy redirect URL that I supply. This redirect never actually goes anywhere, it just shows up as a 404.
However, by monitoring WebBrowser.Url, I can pick up the entire URL that my embedded WebBrowser control is directed to, including the access token that is appended by Salesforce. Basically, after the user authenticates and grants permissions, the embedded browser is redirected to "http://www.dummyurl.com." Salesforce appends the access token, so WebBrowser.Url ends up looking something like this:
http://www.dummyurl.com#access_token=ABCDEF&instance_url=ABCDEF
From here, I can just parse the URL and go on my way. No third-party web server or local web service required. :)
The call the Authorization type you need Authonomous Client http://wiki.developerforce.com/page/Digging_Deeper_into_OAuth_2.0_on_Force.com#Obtaining_a_Token_in_an_Autonomous_Client_.28Username-Password_Flow.29. Read about the URL you have to send there.
grant_type=password&client_id=<your_client_id>&client_secret=<your_client_secret>&username=<your_username>&password=<your_password>
You can use DotNetOpenAuth library. There's an example using WPF, where it uses a winforms control called ClientAuthorizationView provided by DotNetOpenAuth library.
It is a control that hosts a browser allowing the user to authorize the client without leaving the application.
Hope this help.
Regards

talking between Web services and MVC Framework

I am new to MVC and Web Services.
According to my project, I have to show listing data at ViewLayer.
The listing data which I have to show will come from other region via its web service server.
It means that I have to communicate with these web server which is separate with my web application server.
Moreover, my web application have to update some of the data and send this updated data to there web service server again.
That is my project requirement.
So I have searched every possible solutions. Then I found one at stackoverflow.com. According to this, I found that I need to use $.ajax { url: ... } style which I think I need to fully rely on view layer.
Then I had found another solutions which I think I need to fully rely on Controller Layer. I mean I have to write all the code which need to talke with web services only at controller layer.
As I am junior to MVC, I could not decide which one is suitable for me.
Every suggestion will be really appreciated and welcome your any suitable solutions more.
As with all things development - it depends!
If you own the services, they hang off of the same domain, and you're mostly focused on rendering the results of the web service call to HTML, the client-side AJAX calls work well.
If they're on a different domain (or even subdomain), or you want to do more than "just call" the service (e.g., clean up the response, add some tracking, transform it in some way) then handling the web service call via the controller is probably the way to go. You can also easily add server-side caching and logging with this option.
You could use the Unobtrusive Ajax Helpers in MVC3
http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-ajax.html

File Upload to HTTP server from iPhone application. Server side C# API method

Could someone please tell me/link me to how I could create a method similar to those posted below:
http://www.vimeo.com/api/docs/upload
http://www.flickr.com/services/api/upload.api.html
(I am providing the links as I'm not sure how to articulate this question without them!)
I'm using C# ASP.NET. IIS 6.
I have an existing web server with other public API methods. I do not want the iPhone user to have to open a web browser, and post to an aspx page. I want the iPhone developer to be able to call my method, and have one of the parameters be a handle to the file which gets POSTed.
Thanks in advance for any help.
You'll need to create a WCF Service Application. You can use this as a webservice that can be exposed to your clients. You can create a RESTful service using WCF where clients can POST video's to.
When searching for 'REST, API, WCF' you'll probably find all the resources you are looking for.

Categories