Create new instance of a page and obtain URL - c#

i'm a beginner web programmer. I've been coding desktop applications primarily.
Right now, i have created this web app, in Silverlight, that uses a web service of my own for querying the database. The thing is, one of the app functionalities is the ability to open PDF files. I know that silverlight won't let you do this, but using an IFrame on top of the silverlight application you are able to display a page with de pdf file (using acrobat plug in).
So here's the problem, my silverlight app passes the pdf path to the web service and, in return, the web service would create a new Page and pass the new page URI back so that it can be displayed on the IFrame:
Page Code Behing:
public partial class PDFViewer : System.Web.UI.Page
{
string Filename = string.Empty;
public Uri Uri
{
get { return HttpContext.Current.Request.Url; }
}
public PDFViewer(string filename)
{
Filename = filename;
}
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "Application/pdf";
Response.WriteFile(Filename); //Write the file directly to the HTTP content output stream.
Response.End();
}
}
WebMethod Code:
[WebMethod]
public string GetReport(string filename)
{
PDFViewer viewer = new PDFViewer(filename);
return viewer.Uri.AbsoluteUri;
}
this only returns the Web service URL.
So the main question is: How to create a page instance and obtain that page URL?
The solution to this might be here:
http://forums.silverlight.net/forums/p/76977/372282.aspx
"In my Silverlight app i open a new web browser by passing in query string my id and the page process it, query the db, retrieve the selected object and renders with the response methods."
I just don't know how to do it.
Any help is greatly appreciated.
Pedro

Maybe this will help.
wherever your silverlight object is embedded put an iframe with id="xContainer"
and from your silverlight code just set its scr to the pdf you're trying to display
HtmlPage.Document.GetElementById("xContainer").SetProperty("src", "http://google.com");

Related

Close webview xamarin forms

I have implement payment method with return URL and using webview open URL with my app. Recent, I'am issue want auto close webview with response success or fail process payment.
I encountered this case. Here is the solution I found.
After creating the WebElement, we bind the Navigating event to a certain method.
MyPage.Source = link;
MyPage.Navigating += Webview_Navigating;
MyPage WebElment name.
link is my link address
.
Then we perform our transactions here.
private void Webview_Navigating(object sender, WebNavigatingEventArgs e)
{
var url = e.Url;
if (url.Contains("status=1"))
{
int ordID = 0;
//save order
}
}
For me, if the changed link address checkout status=1, the checkout is successful.
You need to call Url property in WebNavigatingEventArgs to catch the changed url
If useful, choose. Good Luck
Sure! You can get response form WebView.
I recommend you use HybridWebView (see: https://github.com/devsmadeofsteel/Plugin.HybridWebView) as alternative.
Examples:
In Xamarin page, create a browser add register a named callback, and add browser to page childs
HybridWebViewControl Browser = new HybridWebViewControl();
//You can load browser content by string or URL, here is string which you can hard code or store in resource files
Browser.ContentType = Plugin.HybridWebView.Shared.Enumerations.WebViewContentType.Internet;
Browser.Source = YourUrl;
Browser.AddLocalCallback(YourCallBackFunctionName, CallBackFunction);
When run, HybridWebViewControl will insert a function with the name you specified in
YourCallBackFunctionName, and the single parameter is string type. So, In JavaScript of the page loaded by YourUrl, you can call back with the specified CallBackFunctionName:
function AnyFunction() {
//...
YourCallBackFunctionName(YourStringParameter);
}
When you call YourCallBackFunctionName(YourStringParameter) in JavaScript, you can get YourStringParameter in CallBackFunction using C#.
So, you could define different CallBackFunctions or pass different parameters with single CallBackFunction, to control the HybridWebView and the page or view which contain it, such as hide HybridWebView or close the page.
Have a try!

Xamarin: how to properly display a webpage reading its source code in an Android app

I am working on an Android application using Xamarin but hopefully the question below is more general and can be answered also by people not familiar with Xamarin.
The app is very simple and has one main page with a list where the use can click on any of the items.
These items represent products and all the related information are stored into a dictionary (key is the product name, values are some information about the product).
Amongst the available fields, I also added a URL of the website where the user can access more information about the product.
The app has a WebView to display the page inside the app.
I can display the webpage by using:
web = FindViewById<WebView>(Resource.Id.webView);
web.SetWebViewClient(new myWebViewClient());
web.Settings.JavaScriptEnabled = true;
web.LoadUrl(item.Link);
where the item/product the user has selected is the item object having the URL saved in item.Link.
The problem is that the user need a web connection in order to load the page. So I thought about saving the whole HTML code into a string so that the user can visualize the content even if no connection is available. I am doing this by:
WebClient wc = new WebClient();
using (Stream st = wc.OpenRead(web.Url))
{
using (StreamReader sr = new StreamReader(st, Encoding.UTF8))
{
html = sr.ReadToEnd();
}
}
Then each item in the dictionary will have a field item.Html with the source code of the page.
I can visualize the page with the following code:
web = FindViewById<WebView>(Resource.Id.webView);
web.SetWebViewClient(new myWebViewClient());
web.Settings.JavaScriptEnabled = true;
//web.LoadUrl(item.Link);
web.LoadData(item.Html, "text/html", "UTF-8");
The problem is that the viewer does not display the page in the same way as web.LoadUrl(item.Link) would do (i.e. the page as you would see in any web browser) but it is much messier and difficult to navigate.
What am I doing wrong? Any better way to achieve my goal?

PhantomJs save webpage containing dynamic data as webpage

I have used jQuery plugin to generate charts on screen along with dynamic data and notes.
I need to generate PDF of this webpage on button click.
I have tried to search good option but not able to find any proper one.
Right now I am trying to use phantomjs for this. It works for internet sites but not working for intranet sites.
I am continuously getting Unable to load address.
Can anyone suggest me the way to achieve this? Or any alternative way to generate PDF from html content (My html contains SVG, Javascript generated dynamic data too).
Code:
string serverPath = "C:\\Phantomjs\\bin\\phantomjs";
var phantomJS = new PhantomJS();
var outFile = Path.Combine(serverPath, "google2.pdf");
if (File.Exists(outFile))
File.Delete(outFile);
try {
phantomJS.Run(Path.Combine(serverPath, "rasterize.js"),
new[] { "http://localhost:61362/HT.aspx?FNo=D1&PrNo=Dummy1&HtId=1033", outFile });
} finally {
phantomJS.Abort();
}
The url is not working in this case. But if I take any internet hosted site, it works.

Spotify authorization code flow (WPF browser control) C#

I'm using Spotify account service (authorization code flow) to get an authorization code by passing in the required parameters (client_id, response_type and redirect_uri. Its a WPF application so i am using the browser control and navigating the user to
https://accounts.spotify.com/authorize/?client_id=myclientId&response_type=code&redirect_uri=someUri
When i copy paste the link in the browser, i see the right stuff i.e page with the Login to Spotify button but when i am navigating through the browser control in my WPF application, it gives me a file download dialog with Authorize.json file to download with Open and Save options, however in some cases it presents the right page to browser code.
Below is my code:
public winOAuthBrowserForm(string navigateTo)
{
InitializeComponent();
webBrowser.Navigated += webBrowser_Navigated;
webBrowser.Navigate(navigateTo);
}
private void webBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
if(!String.IsNullOrEmpty(e.Uri.Query))
{
//since we are looking for code for authorization that will be exchanged for request token from the server
if (e.Uri.Query.StartsWith("?code=") || e.Uri.Query.Contains("code="))
{
code = HttpUtility.ParseQueryString(e.Uri.Query).Get("code");
this.Close();
}
if (e.Uri.Query.StartsWith("?error=") || e.Uri.Query.Contains("error="))
{
error = HttpUtility.ParseQueryString(e.Uri.Query).Get("error");
this.Close();
}
}
}
And i am calling this control like:
var uri = string.Format(SpotifyAuthUriFormatter, RequestAuthBaseUrl, clientId);
winOAuthBrowserForm form = new winOAuthBrowserForm(uri);
form.ShowDialog();
This is what i see:
UPDATE: When i right click on document and see the properties, i see it changes the URL to something like res://ieframe.dll/navcancl.htm#... I've searched it and found some solutions related to I.E (I'm using latest but you can't be sure that it'll be latest on client machines), some say that its firewall setting. The thing is, it appears sometimes only.
Any ideas? Thanks

redirect to a html page of other website c#

I want to redirect to another websites html page in my c# application when a user clicks on some button. I have written this
Response.Redirect("http://www.fashionunic.com/shop/wholesale-m04-dozen-chevron-laced-front-top-medium.html")
But it havent worked it is throwing path is not a valid virtual path
I am calling the below method in page load
Response.Redirect("http://www.fashionunic.com/shop/wholesale-m04-dozen-chevron-laced-front-top-medium.html")
and in
public void Application_BeginRequest()
{
var MyUri = Context.Request.Url;
if(MyUri.Contains("shop/wholesale") {
var String = MyUri.ToString();
Context.RewritePath("http://www.fashionunic.com/shop/wholesale-m04-dozen-chevron-laced-front-top-medium.html");
}
}
Exception:
Invalid path for child request '
http:/www.fashionunic.com/shop/wholesale-m04-dozen-chevron-laced-front-top-medium.html'.
A virtual path is expected.
I've tried your code, you should change 'Context.RewritePath' to 'Response.Redirect'. It should works.
Look here, it maybe useful for you: http://www.techrepublic.com/blog/software-engineer/two-approaches-to-redirection-in-aspnet/
Also, maybe you are marketer and you are trying to promote 'fashionunic' shop :-D

Categories