Create EXE from MVC Application

后端 未结 1 1084
無奈伤痛
無奈伤痛 2021-02-10 08:45

I have created a Game Application using MVC 3 Web Application which is like this

\"enter

1条回答
  •  醉话见心
    2021-02-10 09:25

    Not directly.

    What you can do is use mono xsp to have a simple embedded webserver, which you can put into a .exe, which will then start a webserver on port xy, and open a web-browser with with

    http://localhost:xy/optional-virtual-directory/Home/Game

    you also need to localcopy that webserver-assembly to your web-app's /bin directory for it to work without any installation.

    You'll also need to localcopy all necessary ASP.NET MVC-3 assemblies (because they are most-likely not installed by default).
    And you need to add version 1.0.0 just in case somebody has installed MVC-4 locally.

    And even then, it requires .NET framework 4.0 (or at least 3.5?) installed on the target computer.

    Here a link to the latest stable-XSP sources:
    http://download.mono-project.com/sources/xsp/xsp-2.10.2.tar.bz2

    You can include the zipped web application as embedded resource and use a unzip-library to unzip it to a writeable directory, which you set as your webserver's root directory.

    Make sure your unzip-library does properly unpack JavaScript files, because the microsoft-supplied windows-server windows-explorer-integrated-zip-handling utility does not properly unpack them (may depend on server version and security settings/policy).

    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 
    

    I used this code to get around the missing locale-handling.

    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 Update:

    As per end-2019, you can use .NET Core 3.1, that way you can build a self-contained application, which the user can run without having .NET framework installed at all.

    To build a self-contained .NET Core Application for x86 and x64:

    dotnet restore -r win-x86
    dotnet build -r win-x86
    dotnet publish -f netcoreapp3.1 -c Release -r win-x86
    

    Kestrel is an integrated web-server which you can use instead of Mono.XSP.
    With that, you can run your MVC/.NET-Core Web-Application on port xy (where xy is an unused port-number), and start a web-browser on http(s)://localhost:xy

    0 讨论(0)
提交回复
热议问题