I have .html file inside a directory "/country/main.html". Now, I want to redirect the user to this page whenever a request arrive for any country. I have written a little code snippet
public class Global : System.Web.HttpApplication
{
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
if (app.Request.Path.Contains("/country"))
{
app.Context.RewritePath("/country/main.html");
}
}
}
I tried this url http://www.mydomain.com/country, that works fine. But if the request arrive something like this http://www.mydomain.com/country/new_york.html, it gives me 404 error.
Please suggest, what I have to do in that case?
Related
I'm trying to navigate to another page. I'm using the MVVM pattern. So my button is binded to a command:
private ICommand inscriptionPage;
public ICommand InscriptionPage
{
get
{
if (this.inscriptionPage == null)
this.inscriptionPage = new MyCommand(() => callInscriptionFunction());
return this.inscriptionPage;
}
}
public void callInscriptionFunction()
{
PhoneApplicationPage nav = new PhoneApplicationPage();
nav.NavigationService.Navigate(new Uri("Views/Registration/Registration.xaml", UriKind.Relative));
}
I have this Exception at the last line:
object reference not set to an instance of an object
I check on the web, tried different option, but this error is still there.
Edit: I tried to change the command to put it directly in the code behind. But I have a Debugger.break error.
private void Button_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("Views/Registration/Registration.xaml", UriKind.Relative));
}
Thanks.
First, your Uri should begin with a slash, so change new Uri("Views/Registration/Registration.xaml", UriKind.Relative) to new Uri("/Views/Registration/Registration.xaml", UriKind.Relative). This should make your code behind work.
Second, creating a PhoneApplicationPage is a really strange and wrong idea. If you are not using a MVVM framework that provides navigation service, usu this
App.RootFrame.Navigate(new Uri("/Views/Registration/Registration.xaml", UriKind.Relative))
Is it possible to perform a response.redirect within an iFrame to redirect the whole page so that the destination page is viewable full screen and not contained within the iFrame?
Below is the current code behind
public partial class ServerResult : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
Any help would be much appreciated :-)
No.
Response.Redirect is a server command which sends (302 ?) header tells the client to redirect.
you are in the iframe world. you can't tell the server: "Hey , when you send me data - send it to the parent Iframe"
BUT What you can do , is this :
protected void Page_Load(object sender, EventArgs e) {
ClientScriptManager.RegisterClientScriptBlock(this.GetType(),
"aaa", "window.parent.location = 'http://yoursite.com'", true);
}
but you have to remove response.redirect from server.
Edit
var page = HttpContext.Current.Handler as Page;
if (page != null) page.ClientScript .RegisterClientScriptBlock(typeof(string), "Redirect", "window.parent.location='" + url + "';", true);
Hello I am using this example to login on Facebook using Facebook .net sdk.Eveything works now I have to save the login button state i.e change the button state i.e from log in to log out and also if already connected the display the FB user's name. How can I achieve this?
If saving the state means you want to save information like access_token, profile details etc. Suppose you want to store the access_token only, you can save it to isolated storage:
private void OnSessionStateChanged(object sender, Facebook.Client.Controls.SessionStateChangedEventArgs e)
{
if (e.SessionState == Facebook.Client.Controls.FacebookSessionState.Opened)
{
string fb_access_token = ((Facebook.Client.Controls.LoginButton)sender).CurrentSession.AccessToken;
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings["fb_token"] = fb_access_token ;
settings.Save();
}
}
And in your application you can add a logout button, on its click you can delete access_token from isolated storage.
private void onLogOutClick(object sender, EventArgs e)
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings.Remove("access_token");
settings.Save();
}
On application launch you can check if access_token exist, you can navigate to another page. If not you can redirect to login page.
private void Application_Launching(object sender, LaunchingEventArgs e)
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
if (IsolatedStorageSettings.ApplicationSettings.Contains("fb_token"))
{
// Use this access token in application for fetching other user informations
Uri nUri = new Uri("/Page1.xaml", UriKind.Relative);
RootFrame.Navigate(nUri);
}
else
{
Uri nUri = new Uri("/Login.xaml", UriKind.Relative);
RootFrame.Navigate(nUri);
}
}
customer use http://aaa.com/uploads/bb/index.html to request my ASP.NET MVC site. I want to make sure the customer has enough permission to access the site, so I need catch the request and deal with it. I'm using IHttpModule to do that.
public class myHttpModule : IHttpModule {
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
Uri url = application.Context.Request.Url;
string path = url.AbsoluteUri;
}
}
the question is when second time use the same url to request the website, the HttpModule can't catch the request.
got the answer!It's about the http cache.
I write code as below and the problem was killed:
public class myHttpModule : IHttpModule {
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
Uri url = application.Context.Request.Url;
string path = url.AbsoluteUri;
**app.Response.Cache.SetCacheability(HttpCacheability.NoCache);**
}
}
I need to load a specific webpage from a site that has multiple images on the site. I need to extract these images but I can't do this manually because the names of each image have no pattern and there will be hundreds of sites. I have a silverlight application to load the webpage in an iframe and I intended on extracting the html for this webpage and then retrieving the image source for each image from the extracted code and then populating a listbox.
I can load the web page in iframe with no problem, but I don't know how to retrieve the html code for the webpage.
public Page()
{
InitializeComponent();
System.Windows.Browser.HtmlElement myFrame = System.Windows.Browser.HtmlPage.Document.GetElementById("ifHtmlContent");
if (myFrame != null)
{
myFrame.SetStyleAttribute("width", "1024");
myFrame.SetStyleAttribute("height", "768");
myFrame.SetAttribute("src", txtURI.Text);
myFrame.SetStyleAttribute("left", "0");
myFrame.SetStyleAttribute("top", "50");
myFrame.SetStyleAttribute("visibility", "visible");
}
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
this.Button_Click(sender, e);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
System.Windows.Browser.HtmlElement myFrame = System.Windows.Browser.HtmlPage.Document.GetElementById("ifHtmlContent");
if (myFrame != null) myFrame.SetAttribute("src", txtURI.Text);
}
private void txtURI_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
this.Button_Click(sender, e);
}
The following article may offer some help:
http://jesseliberty.com/2010/05/03/screen-scraping-when-all-you-have-is-a-hammer/