【问题标题】:My first .net Owin program fail to run and show the web page我的第一个 .net Owin 程序无法运行并显示网页
【发布时间】:2016-08-02 16:45:39
【问题描述】:

我关注this example 创建了我的第一个Owin web page

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

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

我现在可以看到 owin 参考。 3、添加->新建项目->Owin启动类并输入代码sn-p:

    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.");
                            });
                    }
            }
    }

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

HTTP 错误 403.14 - 禁止

Web 服务器配置为不列出此目录的内容。

最可能的原因: • 请求的 URL 未配置默认文档,服务器上未启用目录浏览。

您可以尝试的事情: •如果您不想启用目录浏览,请确保配置了默认文档并且该文件存在。 • 启用目录浏览。 1.转到 IIS Express 安装目录。 2.运行 appcmd set config /section:system.webServer/directoryBrowse /enabled:true 启用服务器级别的目录浏览。 3.运行appcmd set config ["SITE_NAME"] /section:system.webServer/directoryBrowse /enabled:true 启用站点级别的目录浏览。

•验证站点或应用程序配置文件中的 configuration/system.webServer/directoryBrowse@enabled 属性是否设置为 true。

详细错误信息:

模块 目录列表模块

通知 执行请求处理程序

处理程序 静态文件

错误代码 0x00000000

请求的网址 http://localhost:50598/

物理路径 D:\Documents\Visual Studio 2013\Projects\WebApplication1

匿名

登录用户 匿名

请求跟踪目录 D:\Documents\IISExpress\TraceLogFiles\WEBAPPLICATION1

更多信息: 如果未在 URL 中指定文档,未为网站或应用程序指定默认文档,并且未为网站或应用程序启用目录列表,则会发生此错误。为了保护服务器的内容,可能会故意禁用此设置。 查看更多信息 »

那么,我在所有步骤中是否遗漏或犯错了什么?如何让它发挥作用?

【问题讨论】:

  • 你安装了 ASP.NET 吗? Windows Features -> Internet Information Services -> World Wide Web Services -> Application Development Features -> ASP.NET x.x.

标签: c# asp.net owin webpage sample


【解决方案1】:

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

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

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

  5. 插入您的 owin 中间件

这就是我的班级的样子:

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>

我什么也没做。这些是我采取的唯一步骤。您可以进行比较,也许可以得出不同之处。

【讨论】:

  • 我与您的文件进行了比较,并检查了我的 packages.config/web.config 是否缺少内容。我创建了另一个解决方案并使用了安装包,我解决了这个问题。感谢您回答这个问题。
猜你喜欢
  • 2014-06-13
  • 2016-04-18
  • 1970-01-01
  • 2018-12-23
  • 2020-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多