【问题标题】:Is WebApi.OwinSelfHost enough to run asp.net mvc app without IIS?WebApi.OwinSelfHost 是否足以在没有 IIS 的情况下运行 asp.net mvc 应用程序?
【发布时间】:2015-12-23 15:47:04
【问题描述】:

some 示例如何使用Owin.SelfHost 制作控制台应用程序。但我没有找到任何关于自托管的 ASP.NET MVC 应用程序的信息。有可能吗?
我想在 asp.net 技术栈中实现 node-webkit 的可能性。

This 的问题很相似。有什么改变吗?有人使用 ASP.Net 5 MVC 6 RC1 有结果吗?

【问题讨论】:

    标签: asp.net asp.net-mvc owin self-hosting


    【解决方案1】:

    答案是你问题的第一部分是否定的。

    直到现在在 ASP.NET 5 MVC 6 中的 vNext 版本才能自托管 ASP.NET MVC。

    在对此进行广泛研究后,我不得不使用 Nancyfx 在 .net 4.0 环境中满足这一要求,或者只是使用自托管 Web API 开发一个 spa,并将我的静态内容嵌入到 .net 4.5 及更高版本中。

    注意:我有效地使用 NancyFX 的方式与使用 Web API 的方式相同(我有一个 NancyFX 和 Web API 项目从一个常见的客户端 SPA 项目中提供嵌入式内容),它仅取决于 .net 的版本。下面是 Web API 项目的一个小例子

    // In the startup class
    // Catch all route for all embedded files!
    config.Routes.MapHttpRoute(
        name: "FilesApi",
        routeTemplate: "{folder}/{*file}",
        defaults: new {controller = "File", file = RouteParameter.Optional},
        constraints: new {controller = @"^(?:(?!Some|exceptions|like|api).)*$"}
        );
    
    //My embedded resource files:
    var resources = Assemblies.SelectMany(a => a.GetManifestResourceNames()).ToArray();
    //To server static content for html I use
    protected static HttpResponseMessage GetHtml(string htmlFile)
    {
        var fileName = resources.FirstOrDefault(r => r.EndsWith(htmlFile, StringComparison.CurrentCultureIgnoreCase));
        const string mediaType = "text/html";
    
        if (string.IsNullOrEmpty(fileName))
            return null;
        var result = getResourceText(fileName);
    
        var responseMessage = new HttpResponseMessage
        {
            Content = new StringContent(result, System.Text.Encoding.UTF8, mediaType)
        };
        return responseMessage;
    }
    
    //For everything else I use:
    protected static HttpResponseMessage getEmbeddedFile(string file)
    {
        var mediaType = getMediaType(file); // NancyFX makes this much easier with their  MimeTypes.GetMimeType(file) helper method
        var ms = getResourceStream(file); //.Save(ms, ImageFormat.Png);
        var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(ms.ToArray()) };
        result.Content.Headers.ContentType = new MediaTypeHeaderValue(mediaType);
        return result;
    }
    
    protected static MemoryStream getFileStream(string filename)
    {
        var result = new MemoryStream();
        try
        {
            using (var stream = new FileStream(filename, FileMode.Open))
            {
                stream.CopyTo(result);
                result.Position = 0;
            }
        }
        catch (Exception ex)
        {
            Log.Error(ex);
            throw;
        }
        return result;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-29
      • 2021-09-24
      • 1970-01-01
      • 2014-03-19
      • 2014-10-23
      • 2011-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多