c# - 从 MVC 应用程序创建 EXE

标签 c# asp.net-mvc-3

我已经使用 MVC 3 Web 应用程序创建了一个游戏应用程序,就像这样

enter image description here

这个 Controller / Action 是 Like\Home\Game

我想知道是否可以将此 MVC 应用程序转换为 EXE 文件,以便任何用户都可以在他的 PC 上运行它。我知道我们可以为 Windows 应用程序创建 EXE 文件,Web 应用程序可以吗?

最佳答案

不直接。

您可以做的是使用单声道 xsp 来拥有一个简单的嵌入式网络服务器,您可以将其放入一个 .exe 文件中,然后它将在端口 xy 上启动一个网络服务器,并使用以下命令打开一个网络浏览器
http://localhost:xy/optional-virtual-directory/Home/Game
您还需要将该网络服务器程序集本地复制到您的网络应用程序的/bin 目录,以便它无需任何安装即可工作。

您还需要本地复制所有必需的 ASP.NET MVC-3 程序集(因为它们很可能默认未安装)。
并且您需要添加版本 1.0.0,以防万一有人在本地安装了 MVC-4。

即便如此,它也需要在目标计算机上安装 .NET framework 4.0(或至少 3.5?)。

这是指向最新稳定 XSP 源的链接:
http://download.mono-project.com/sources/xsp/xsp-2.10.2.tar.bz2

您可以将压缩的 Web 应用程序包含为嵌入式资源,并使用解压缩库将其解压缩到可写目录,您将其设置为 Web 服务器的根目录。

确保您的 unzip-library 正确解压 JavaScript 文件,因为微软提供的 windows-server windows-explorer-integrated-zip-handling 实用程序无法正确解压它们(可能取决于服务器版本和安全设置/策略)。

static void Main()
{

    int iPort = 8080; // If admin rights it requires, wrong it is ;)
    iPort = 30080; // Damn !  I still no haz no admin rightz !


    string strBasePath = @"D:\UserName\documents\visual studio 2010\Projects\EmbeddableWebServer\TestApplication";

    string strCurrentDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
    System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(strCurrentDirectory);
    //strBasePath = System.IO.Path.Combine(di.Parent.Parent.Parent.FullName, "TestApplication");
    strBasePath = System.IO.Path.Combine(di.Parent.Parent.Parent.FullName, "TestMvcApplication");

    //EmbeddableWebServer.cWebSource WebSource = new EmbeddableWebServer.cWebSource(System.Net.IPAddress.Any, iPort);
    Mono.WebServer.XSPWebSource ws = new Mono.WebServer.XSPWebSource(System.Net.IPAddress.Any, iPort);

    // EmbeddableWebServer.cServer Server = new EmbeddableWebServer.cServer(WebSource, strBasePath);
    Mono.WebServer.ApplicationServer Server = new Mono.WebServer.ApplicationServer(ws, strBasePath);

    Server.AddApplication("localhost", iPort, "/", strBasePath);
    Server.Start(true);


    System.Diagnostics.Process.Start("\"http://localhost:" + iPort.ToString() + "\"");

    Console.WriteLine(" --- Server up and running. Press any key to quit --- ");
    Console.ReadKey();

    Server.Stop();
} // End Sub Main 

我使用此代码来解决缺少的语言环境处理。
using System;
using System.Collections.Generic;
using System.Text;


namespace System
{


    public class Locale
    {
        // http://msdn.microsoft.com/en-us/library/441722ys(v=vs.80).aspx
        // #pragma warning disable 414, 3021

        public static string GetText(string message)
        {
            return message;
        }


        public static string GetText(string format, params object[] args)
        {
            return string.Format(format, args);
        }


        /*
        public static object GetResource(string name)
        {
            return name;
        }
        */


    } // End Class Locale


} // End Namespace System

2019 更新:

到 2019 年底,您可以使用 .NET Core 3.1,这样您就可以构建一个自包含的应用程序,用户无需安装 .NET 框架即可运行该应用程序。

为 x86 和 x64 构建独立的 .NET Core 应用程序:
dotnet restore -r win-x86
dotnet build -r win-x86
dotnet publish -f netcoreapp3.1 -c Release -r win-x86

Kestrel 是一个集成的 Web 服务器,您可以使用它来代替 Mono.XSP。
这样,您就可以在端口 xy(其中 xy 是未使用的端口号)上运行您的 MVC/.NET-Core Web 应用程序,并在 http(s)://localhost:xy 上启动 Web 浏览器。

关于c# - 从 MVC 应用程序创建 EXE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19288927/

相关文章:

c# - 如何将属性绑定(bind)到 WPF 中 Treeview 中的选定节点

c# - 是否可以隐藏(而不是删除)PivotItem?

c# - 在带有 Winforms 的 Visual Studio 2010 中将字符串与控件相关联的编程高效方法

c# - MVC 存储库 - 域模型与实体模型

model-view-controller - ColdFusion Model Glue 与 ASP.NET MVC 3 的 @section 的等效项是什么?

C# 字典相等要求

c# - 在 View 中创建新的 SelectList 时未设置 DropDownListFor 值

c# - MVC 3 适合公共(public)生产站点吗?

c# - asp.net mvc 3 c# post 变量数组

ajax - MVC 中 Ajax 调用 Partial View 方法报错