Go Back

Using Silverlight initParams with ASP.NET MVC

I looked around and couldn't really find a clean way to add init params to the view of my mvc silverlight host aspx file. Here's what i ended up doing... just wanted to blog it so that i didn't forget it :)


<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<script src="../../Silverlight.js" type="text/javascript"></script>
    <object height="100%" width="100%" data="data:application/x-silverlight-2," type="application/x-silverlight-2" >
 <param name="source" value="../ClientBin/PalladiumEngine.xap"/>
 <param name="onError" value="onSilverlightError" />
 <param name="background" value="white" />
 <param name="EnableGPUAcceleration" value="true" />
 <%=ViewData["InitParams"] %>
 <param name="minRuntimeVersion" value="3.0.40624.0" />
 <param name="autoUpgrade" value="true" />
 <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration:none">
   <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
 </a>
   </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe>
</asp:Content>


then my Controller action code looked like this:

  public ActionResult Index()
        {
            string port = WebConfigurationManager.AppSettings["PORT"];
            if(string.IsNullOrEmpty(port))
                throw new ApplicationException("Put a PORT value into the web.config please!");
            ViewData["InitParams"] = string.Format("<param name=\"initParams\" value=\"Port={0}\" />",port);
            ActionResult retval = View();
            return View();
        }

then my silverlight code using it looked like this:
     private void Application_Startup(object sender, StartupEventArgs e)
        {
            int port = int.Parse(e.InitParams["Port"]);
            Client = new MessageClient<SocketMessage>(port, Serializer);
            Client.ConnectCompleted += OnConnected;
            Client.MessageRecieved += new EventHandler<MessageRecievedEventArgs<SocketMessage>>(client_MessageRecieved);
            Client.ConnectAsync();

            RootVisual = MainPage;
            MainPage.LayoutRoot.Children.Clear();
            MainPage.LayoutRoot.Children.Add(LoginPage);
        }


I was using it to pass the port into silverlight from my web.config. For security reasons be sure you don't do this with user specified content!

Comments  1

  • sikat ang pinoy 2/5/2010 12:00:00 AM

    Creating silverlight on asp.net mvc are now easy. Thanks for the tutorials I like how you have presented the information in full detail. Keep up the great work
Post a comment!
  1. Formatting options