简体   繁体   English

在ASP .NET MVC 4中运行Owin应用程序

[英]Run Owin app in ASP .NET MVC 4

I have an ASP .NET MVC 4 project where I am trying to integrate an Owin app to run only for a specific path, so all requests starting with owin-api/* will be handled by the Owin pipeline Microsoft.Owin.Host.SystemWeb.OwinHttpHandler and the other requests by the MVC pipeline System.Web.Handlers.TransferRequestHandler 我有一个ASP .NET MVC 4项目,我试图将Owin应用程序集成到仅运行特定路径,因此所有以owin-api / *开头的请求都将由Owin管道Microsoft.Owin.Host.SystemWeb处理。 .OwinHttpHandler和MVC管道的其他请求System.Web.Handlers.TransferRequestHandler

To accomplish this, I have the following: 为此,我有以下内容:

In the Web.config Web.config中

<appSettings>
    <add key="owin:appStartup" value="StartupServer.Startup"/>
</appSettings>   
<system.webServer>
        <handlers>
            <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
            <remove name="OPTIONSVerbHandler" />
            <remove name="TRACEVerbHandler" />
            <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
            <add  name="Owin" verb="*" path="owin-api/*" type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler, Microsoft.Owin.Host.SystemWeb" />
        </handlers>
</system.webServer>

The startup class: 启动类:

namespace StartupServer
{

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.Run(context =>
            {
                return context.Response.WriteAsync("Owin API");
            });
        }
    }
}

However "Owin API" is now the outoput for every request. 然而,“Owin API”现在是每个请求的outoput。 How can I tell IIS to use the OwinHttpHandler only when the path owin-api/* as specified in the Web.config? 如何在IIS.in中指定的路径owin -api / *时告诉IIS使用OwinHttpHandler?

app.Run() inserts into the OWIN pipeline a middleware which does not have a next middleware reference. app.Run()在OWIN管道中插入一个没有下一个中间件引用的中间件。 So you probably want to replace it with app.Use() . 所以你可能想用app.Use()替换它。

You could detect the URL and base your logic on that. 您可以检测到URL并将其逻辑基于此。 For example: 例如:

app.Use(async (context, next) =>
{
    if (context.Request.Uri.AbsolutePath.StartsWith("/owin-api"))
    {
        await context.Response.WriteAsync("Owin API");
    }
    await next();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM