【问题标题】:MVC download of clickonce Setup.exe and display confirmation viewclickonce Setup.exe的MVC下载并显示确认视图
【发布时间】:2016-10-21 19:26:50
【问题描述】:

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

我创建了一个新控制器(用户)和几个视图(下载)和(设置)。我创建了下载视图使用的下载模型。

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

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 以最初显示下载视图,然后在 Post 中检查是否选中了所有复选框,如果没有,它会更新响应消息并返回视图。 如果所有复选框都被选中,那么它会拉出订阅者(它必须存在,因为它是在用户通过帐户控制器 - 身份管理验证他们的电子邮件时创建的),然后继续使用原始订阅者(如果是新的)更新订阅者或上次下载日期。此时我要开始下载 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# 代码可能过于冗长,而且我的做法可能是完全错误的,但我只是在争先恐后地完成这件事,以便我可以部署我的用于选择 Beta 用户进行测试的 WPF 应用程序。靠积蓄谋生已经很长时间了,我可以从这里闻到生活气息。

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

感谢所有意见和建议。

【问题讨论】:

    标签: asp.net-mvc clickonce


    【解决方案1】:

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

    <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 命名为我想要的任何名称。奖金:)

    【讨论】:

    • 这适用于测试文本文件,但是当我从 WPF 应用程序的 clickonce 部署切换到 setup.exe 文件时,我遇到了更多问题。它下载并运行,安装了先决条件,但找不到 xxx.application 文件。我将在下周继续处理这个问题,并在我得到答案后立即发布更新。
    • 好的 - 我已经解决了这个问题。确保 1) 发布/安装文件夹设置为上传应用程序的正确 http(s)://xxxx 文件夹,以及 2) 发布/更新/更新位置文件夹也设置为相同文件夹 (即使您没有使用自动更新)。 Setup.exe 将找到 xxx.application 文件夹,您就可以开始了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多