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!

Facebook DZone It! Digg It! StumbleUpon Technorati Del.icio.us NewsVine Reddit Blinklist Furl it!

Comments  2

  • 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
  • Chris 1/16/2011 12:00:00 AM

    I "also" looked around and couldn't really find a clean way to add init params to the view of my mvc silverlight host aspx file, as you mentioned.  Your solution is quite simple and elegant.  I was able to implement it in a few minutes.  Thank you so much for sharing your code.

    Best regards,
    Chris
Post a comment!
  1. Formatting options