Follow us on X (Twitter)  Like us on Facebook  Connect with us on LinkedIn  Subscribe to our YouTube Channel  Subscribe to our WhatsApp Group

How to Navigate to an URL in LightSwitch?


If you are working on a LightSwitch application and looking for a way to navigate to an URL, this post will help you to understand and integrate the same in your project. - Article authored by Kunal Chowdhury on .

Sometime we might want to navigate to a specific URL in LightSwitch application. That might be a web page URL or a direct URL to some file. If you are a Silverlight developer, you might know how to navigate to an URL but there are some trick in case of LightSwitch.

 

If you are looking for a piece of code to learn the mechanism and to include in your project, then this post will guide you step-by-step.

 

 

Browser Application

If you are building a browser based LightSwitch application, you can just call the “HtmlPage.Window.Navigate(…)” method present under the “System.Windows.Browser” namespace. You will just need to add the dll reference of “System.Windows.Browser.dll” to make the piece of code to work.

 

Make sure to surround the call with a Dispatcher, so that, it should not throw cross thread exception. To do this, you have to call the “Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(…)” method as demonstrated below:

 

Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
{
    HtmlPage.Window.Navigate(new Uri("http://www.kunal-chowdhury.com"), "_blank");
});

 

The Navigate() method takes two parameters. First one is the Uri object and the second one is the optional settings like target frame. If you pass “_self” as the second parameter, it will open the URL in the same page. If you want it to open in a new tab, pass “_blank” as the second parameter there (as mentioned above).

 

 

Out-of-Browser Application

It’s not mandatory to build a browser based LightSwitch application. By default, it creates an out-of-browser based application in LightSwitch. If you are building the app to run outside browser window, the above code will not work. It will throw an “InvalidOperationException” with the following message:

The DOM/scripting bridge is disabled.

This exception will come when you are running LightSwitch application in out-of-browser mode i.e. as desktop client. In that case, if you call the “HtmlPage.Window.Navigate()” method, it will throw this exception because the DOM/Scripting bridge becomes disabled by design.

 

This is not an issue with LightSwitch but an issue with Silverlight. Silverlight restricts user for doing such operations outside the browser window due to security reason. As LightSwitch is nothing but a part of Silverlight, this also poses the same security issue.

 

So, what to do in such case? To resolve this issue, you have to do an workaround by creating an object of “Shell.Application” by using the AutomationFactory and then calling the “ShellExecute(…)” method by passing the URL as shown below:

 

Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
{
    dynamic shell = AutomationFactory.CreateObject("Shell.Application");
    shell.ShellExecute("http://www.kunal-chowdhury.com");
});

 

Make sure to put that piece of code under the “Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(…)” method as demonstrated above. This will resolve the cross thread exception in the code.

 

 

Support both In-Browser and Out-of-Browser call

Let’s see how we can enhance the code to support both In-Browser and Out-of-Browser applications to navigate to an URL. To do this, check if the Automation Factory is available. If it is there, that means you are running it outside browser and thus you have to create object from AutomationFactory and then execute the shell to navigate to that particular URL.

 

If automation factory is not available, but your application is running inside the browser window, you can directly call the HtmlPage.Window.Navigate() method by passing the URL as parameter to it.

 

If both the condition fails, you can throw an InvalidOperationException to let the user know that something wrong happened right now. Here is the complete piece of code for your reference:

 

Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
{
    if (AutomationFactory.IsAvailable)
    { 
        dynamic shell = AutomationFactory.CreateObject("Shell.Application");
        shell.ShellExecute("http://www.kunal-chowdhury.com");
    }
    else if (!System.Windows.Application.Current.IsRunningOutOfBrowser)
    {
        HtmlPage.Window.Navigate(new Uri("http://www.kunal-chowdhury.com"), "_blank");
    }
    else
    {
        throw new InvalidOperationException();
    }
}); 
    

 

Are you still facing issue? Leave a line below and I will try to help you as soon as I can. Share it to your friends available on Twitter, Facebook or any other network.

 

Don’t forget to subscribe to my blog’s RSS Feed and Email Newsletter to receive regular article updates delivered directly to your inbox. I am available on Twitter, Facebook and Google+. Connect with me and say a “Hello”.

Have a question? Or, a comment? Let's Discuss it below...

dhgate

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.