MVC下载clickonce Setup.exe并显示确认视图

时间:2016-10-21 19:26:51

标签: asp.net-mvc clickonce

我首先要说的是我是一名C#MVC新手,但我已经建立了一个带有身份管理的网站,并使用一些自定义表扩展数据库以存储有关我的用户的其他信息,所以我不是完全新手。我一直在研究一个VB WPF应用程序,我想从我的新网站部署,这就是我遇到问题的地方。

我创建了一个新的控制器(用户)和几个视图(下载)& (建立)。我创建了下载视图使用的downloadmodel。

在抽象中我正在做的是显示下载视图(获取),其中有三个复选框以确认用户已阅读概述,安装和服务条款。这些是模型中的布尔值。我还在模型中有一个字符串响应消息,它显示在提交按钮的正上方。这是模型:

public class DownloadModel
{
    public bool Overview { get; set; }
    public bool Installation { get; set; }
    public bool TermsOfService { get; set; }
    public string Response { get; set; }

    public DownloadModel()
    {
        Overview = false;
        Installation = false;
        TermsOfService = false;
        Response = "After checking boxes click the button to begin installation";
    }

}

我的用户控制器处理Get to最初显示下载视图,然后在Post it检查以查看是否勾选了所有复选框,如果没有,它会更新响应消息并返回视图。 如果选中所有复选框,则它会拉取订户(必须存在,因为它是在用户通过帐户控制器验证其电子邮件时创建的 - 身份管理),然后继续使用原始订户(如果是新的)更新订户或上次下载日期。此时,我想在返回设置视图之前开始下载clickonce setup.exe文件。

[Authorize]
public class UserController : Controller
{
    // GET: User/Download
    public ActionResult Download()
    {
        return View(new DownloadModel { });
    }

    // Post: User/Download
    [HttpPost]
    public ActionResult Download(DownloadModel downloadcheck)
    {
        if (!ModelState.IsValid)
        {
            return View(downloadcheck);
        }

        //check to see if all the boxes were checked
        if (downloadcheck.Overview == true &
                        downloadcheck.Installation == true &
                        downloadcheck.TermsOfService == true)
        {
            //yes - so let's proceed 
            //first step is to get the subscriber
            Subscriber tSubscriber = new Subscriber();
            tSubscriber.Email = User.Identity.Name;
            bool okLoad = tSubscriber.LoadByEmail();
            if (okLoad == false)
            {
                //we have a real problem. a user has logged in but they are not yet
                //a valid subscriber?
                throw new Exception("Subscriber not found");

            }

            // update subscriber with download in process...
            if (tSubscriber.OriginalDownload == DateTime.MinValue)
            {
                tSubscriber.OriginalDownload = DateTime.Now;
                tSubscriber.LastDownload = tSubscriber.OriginalDownload;
            }
            else
            {
                tSubscriber.LastDownload = DateTime.Now;
            }
            if (tSubscriber.UpdateDownloaded() == false)
            {
                //update of download dates failed
                //another problem that shouldnt occur.
                downloadcheck.Response = "A problem occured downloading your setup."
                    + "Try again. If this error continues please contact support.";
                return View(downloadcheck);
            }

            //download dates have been updated for the subscriber so let's start the download!

            //THIS IS WHERE I NEED TO BEGIN THE DOWNLOAD

            return View("Setup");
        }
        else
        {
            // all boxes were not checked - update message
            downloadcheck.Response = "Please confirm you have reviewed the above information " 
                + "by checking all of the boxes before clicking on the button.";
            return View(downloadcheck);
                        }

    }


}

下载视图非常简单,设置视图只是确认下载已启动,并提供了指向帮助设置页面的链接。

我真的有点迷失在这里。我以为我会插入一个返回的新文件路径响应,但我不能这样做并返回设置视图。

我的另一个想法是以某种方式从设置视图中触发我的/xxx/setup.exe的下载,因为它被返回 - 但我不知道如何实现这一点。

我会第一个承认我的mvc c#代码可能过于冗长,而我对我如何做到这一点的方法可能完全错误,但我只是忙着完成这项工作以便我可以部署WPF应用程序选择Beta用户进行测试。这是一段很长时间的积蓄生活,我可以从这里闻到上线。

最后一点,我正在使用setup.exe clickonce部署我的wpf应用程序以简化,因为有.net和localsqldb先决条件,但我不会使用自动更新 - 而不是这是真的相关。

感谢所有意见和建议。

1 个答案:

答案 0 :(得分:0)

经过更多挖掘和黑客攻击后,我找到了一个有效的解决方案。首先在我的设置视图(确认页面)中,我添加了一个简单的脚本来启动用户控制器中的新功能:

<script type="text/javascript">
    window.location.href = "/user/sendfile/"
</script>

控制器更改也很简单。为了测试,我只使用了一个txt文件。

//  User/SendFile
    public ActionResult SendFile()
    {
        string path = @"~/xxxx/anyfile.txt";
        string content = "application/txt";
        //string content = "application/x-ms-application";
        return new FilePathResult(path, content)
        {
            FileDownloadName = "mynewtext.txt"
        };
    }

此解决方案真正有趣的是FileDownloadName是文件内容的下载内容。因此,通过这种方式,我可以参考路径中的实际setup.exe文件,然后将下载的exe命名为我想要的任何内容。奖金:)

相关问题