gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:32:00 24 4
gpt4 key购买 nike

我关注了this example创建我的第一个 Owin 网页:

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

    PM> 安装包 microsoft.owin.host.SystemWeb

我现在可以看到 owin 引用。3、添加->New Item->Owin启动类并输入代码段:

    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 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 »

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

最佳答案

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

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

  4. 在解决方案资源管理器中右键单击项目 > 添加 > 类并将其命名为 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>

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

关于c# - 我的第一个 .net Owin 程序无法运行并显示网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38714132/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com