将值从 ASP.NET 传递到 Silverlight 应用程序



我在 Microsoft Visual Web Developer 2010 Express 的 .web 部件上创建了一个 ASP.NET 项目,并包含一个 Silverlight 项目。我已经创建了一个类似音乐博客的东西,我想将"path"值传递给Silverlight。用户上载文件,曲目将在 Silverlight 应用中播放。

例如,我在数据库中创建了一个名为Posts的表和另一个从名为Track的帖子派生的表,其中存储了文件的路径。我还在我的索引.aspx文件中包括:

<form id="form1" runat="server" style="height:50%">
<div id="silverlightControlHost">
    <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="770" height="530">
      <param name="source" value="ClientBin/MusicBlog.xap"/>
      <param name="onError" value="onSilverlightError" />
      <param name="background" value="white" />
      <param name="minRuntimeVersion" value="3.0.40818.0" />
      <param name="autoUpgrade" value="true" />
      <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0" style="text-decoration:none">
          <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Holen Sie sich Microsoft Silverlight" style="border-style:none"/>
      </a>
    </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
</form>

创建帖子时,我调用:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(Posts model, HttpPostedFileBase file)
    {
        if (ModelState.IsValid)
        {
            if (file != null)
            {
                if (file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/Music"), fileName);
                    file.SaveAs(path);
                    Track track = new Track();
                    track.Path = path;
                    model.Track.Add(track);
                    DateTime today = DateTime.Today;
                    Posts post = new Posts();
                    /*post.Body = model.Body;
                    post.Created = model.Created;
                    post.Modified = model.Modified;
                    post.Title = model.Title;*/
                    model.Created.ToLocalTime();
                    postRepository.Create(model);
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError("", "The given Path is invalid");
                }
            }
            else
            {
                ModelState.AddModelError("", "The given Path is invalid");
            }
        }
        return View(model);
    } 

现在如何将路径传递给 Silverlight 中的播放器,以便我可以在嵌入在 ASP.NET 页面的 Silverlight 应用程序中播放曲目?

为 Index 创建重载

public ActionResult Index(string trkPath) { }

并在重定向时添加路由值

return(RedirectToAction("Index", new { trkPath = track.Path }));

并访问 Silverlight 中的参数

NavigationContext.QueryString["trkPath"]
要使

Silverlight 访问本地文件系统上的轨道,用户必须启动选择,您不能"为他们"访问文件系统。 您可以在以提升的信任运行的 Silverlight 浏览器外应用程序中(您实际上可以在浏览器中的 Silverlight 5 中执行此操作,但这是一个边缘情况)。

为什么不让 Silverlight 应用程序提示输入曲目并上传它呢?

相关内容

最新更新