我的第一个 .net Owin 程序无法 运行 并显示网页

My first .net Owin program fail to run and show the web page

我跟着 this example 创建了我的第一个 Owin web page:

  1. 启动VS2013,创建Web应用程序的C#项目:WebApplication1.
  2. 工具->Nuget包管理器->包管理器控制台

    PM> Install-Package microsoft.owin.host.SystemWeb

我现在可以看到 owin 参考资料了。 3. Add->New Item->Owin startup class并输入代码片段:

    using System;
    using System.Threading.Tasks;
    using Microsoft.Owin;
    using Owin;

    [assembly: OwinStartup(typeof(WebApplication1.Startup1))]

    namespace WebApplication1
    {
            public class Startup1
            {
                    public void Configuration(IAppBuilder app)
                    {
                            // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
                            app.Run(context =>
                            {
                                    context.Response.ContentType = "text/plain";
                                    return context.Response.WriteAsync("Hello, world.");
                            });
                    }
            }
    }

OK,现在我Ctrl+F5启动,弹出IE浏览器。不幸的是,该页面显示 Web 应用程序内部存在一些错误:

HTTP Error 403.14 - Forbidden

The Web server is configured to not list the contents of this directory.

Most likely causes: •A default document is not configured for the requested URL, and directory browsing is not enabled on the server.

Things you can try: •If you do not want to enable directory browsing, ensure that a default document is configured and that the file exists. • Enable directory browsing. 1.Go to the IIS Express install directory. 2.Run appcmd set config /section:system.webServer/directoryBrowse /enabled:true to enable directory browsing at the server level. 3.Run appcmd set config ["SITE_NAME"] /section:system.webServer/directoryBrowse /enabled:true to enable directory browsing at the site level.

•Verify that the configuration/system.webServer/directoryBrowse@enabled attribute is set to true in the site or application configuration file.

Detailed Error Information:

Module DirectoryListingModule

Notification ExecuteRequestHandler

Handler StaticFile

Error Code 0x00000000

Requested URL http://localhost:50598/

Physical Path D:\Documents\Visual Studio 2013\Projects\WebApplication1

Anonymous

Logon User Anonymous

Request Tracing Directory D:\Documents\IISExpress\TraceLogFiles\WEBAPPLICATION1

More Information: This error occurs when a document is not specified in the URL, no default document is specified for the Web site or application, and directory listing is not enabled for the Web site or application. This setting may be disabled on purpose to secure the contents of the server. View more information »

所以我在所有的步骤中有没有遗漏或错误的地方?如何让它发挥作用?

你的例子对我来说很好用。我无法重现您的错误。似乎配置错​​误。也许尝试检查一些现有的答案 and here。我包括了我的步骤,这些步骤对我来说非常好,因此您可以仔细检查您的解决方案。

  1. 文件 > 新建 > 项目 > Web
  2. 创建完全空的 Web 应用程序
  3. 安装Microsoft.Owin.Host.SystemWeb包: PM> install-package microsoft.owin.host.systemweb

  4. 在解决方案资源管理器中右键单击项目 > 添加 > Class 并将其命名为 Startup.cs

  5. 插入你的owin中间件

这是我的 class 的样子:

using Owin;

namespace WebApplication2
{
    public class Startup
    {
        public static void Configuration(IAppBuilder app)
        {
            app.Use(async (ctx, next) =>
            {
                await ctx.Response.WriteAsync("Test");
            });
        }
    }
}

为了确保,我也包括了我的 packages.config:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net452" developmentDependency="true" />
  <package id="Microsoft.Owin" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net452" />
  <package id="Owin" version="1.0" targetFramework="net452" />
</packages>

..和web.config:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2"/>
    <httpRuntime targetFramework="4.5.2"/>
  </system.web>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
</configuration>

除此之外我什么都没做。这些是我采取的唯一步骤。大家可以比较一下,或许可以找出不同之处。