繁体   English   中英

URL重写模块(IIS)无法与IIS上托管的OWIN中间件一起使用

[英]URL Rewrite Module (IIS) not working with OWIN Middleware hosted on IIS

如何将URL重写模块与OWIN中间件一起使用?

阅读http://www.asp.net/aspnet/overview/owin-and-katana/owin-middleware-in-the-iis-integrated-pipeline后 ,我说我应该能够在IIS管道中使用OWIN中间件,使用NuGet软件包:

Microsoft.Owin.Host.SystemWeb

这是我尝试使用的代码:

Web.config:

<system.webServer> <rewrite> <rules> <rule name="pandainserter" patternSyntax="ECMAScript" stopProcessing="true"> <match url=".*test.*" /> <conditions trackAllCaptures="true"> <add input="{REQUEST_URI}" pattern="/(.*?)/(.*)" /> </conditions> <action type="Rewrite" url="http://{HTTP_HOST}:20000/v1/{C:2}" appendQueryString="false" /> </rule> </rules> </rewrite> </system.webServer>

Startup.cs(OWIN):

[assembly: OwinStartup(typeof(ApiUrlRewriter.Startup))]
namespace ApiUrlRewriter
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.Run(context =>
            {
                string t = DateTime.Now.Millisecond.ToString();
                return context.Response.WriteAsync(t + " Production OWIN   App");
            });
        }
    }
}

我想要做的是使用OWIN中间件,以允许CORS并修复OPTIONS飞行前的HTTP调用(此处描述: 如何在WebAPI 2中进行CORS身份验证? ),同时仍然具有使用IIS Rewrite模块进行重写的能力。我对同一服务器上其他站点的呼叫。

对于所有通话,我都会收到此错误。 将OWIN与IIS Rewrite模块一起使用时:

HTTP错误404.4-找不到您要查找的资源没有与之关联的处理程序。

详细错误信息:

IIS Web核心模块

通知MapRequestHandler

处理程序ExtensionlessUrl-Integrated-4.0

错误代码0x8007007b

似乎重写模块未注册,我不确定自己做错了什么,还是什至使用正确的方法?

首先,默认情况下, IIS URL Rewrite模块似乎在IIS Express上不起作用(我没有尝试在本地开发计算机上安装它)。

因此,在装有IIS和IIS URL重写模块的Windows Server 2012上运行此程序时,我可以看到我的HTTP请求开始被重写。

为了进一步参考,我发布了用于解决CORS Preflight问题的代码,同时还具有将HTTP请求重写到其他服务器的可能性。 请注意,这将允许来自任何来源的CORS请求。

Startup.cs类:

using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(Startup))]

namespace ApiUrlRewriter
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.Use<CorsPreflightMiddlerware>();
            app.Use<CorsAuthorizationMiddleware>();
        }
    }
}

CorsPreflightMiddlerware.cs类:

public class CorsPreflightMiddlerware : OwinMiddleware
{
    private const string HTTP_METHOD_OPTIONS = "OPTIONS";

    public CorsPreflightMiddlerware(OwinMiddleware next)
        : base(next)
    {
    }

    public async override Task Invoke(IOwinContext context)
    {
        IOwinRequest req = context.Request;
        IOwinResponse res = context.Response;

        if (req.Method == HTTP_METHOD_OPTIONS)
        {
            res.StatusCode = (int)HttpStatusCode.OK;
            res.Headers.AppendCommaSeparatedValues("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, PATCH, OPTIONS");
            res.Headers.AppendCommaSeparatedValues("Access-Control-Allow-Headers", "Content-Type, Authorization");
        }
        else
        {
            await Next.Invoke(context);
        }
    }
}

CorsAuthorizationMiddleware.cs类:

public class CorsAuthorizationMiddleware : OwinMiddleware
{
    public CorsAuthorizationMiddleware(OwinMiddleware next)
        : base(next)
    {
    }

    public async override Task Invoke(IOwinContext context)
    {
        IOwinRequest req = context.Request;
        IOwinResponse res = context.Response;

        var origin = req.Headers.Get("Origin");
        if (!string.IsNullOrEmpty(origin))
        {
            res.Headers.Set("Access-Control-Allow-Origin", origin);
        }
        if (string.IsNullOrEmpty(res.Headers.Get("Access-Control-Allow-Credentials")))
        {
            res.Headers.Set("Access-Control-Allow-Credentials", "true");
        }

        res.Headers.AppendCommaSeparatedValues("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, PATCH, OPTIONS");
        res.Headers.AppendCommaSeparatedValues("Access-Control-Allow-Headers", "Content-Type, Authorization");

        await Next.Invoke(context);
    }
}

Web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="test" patternSyntax="ECMAScript" stopProcessing="true">
          <match url=".*test.*" />
          <conditions trackAllCaptures="true">
            <add input="{REQUEST_URI}" pattern="/(.*?)/(.*)" />
          </conditions>
          <action type="Rewrite" url="http://{HTTP_HOST}:20000/v1/{C:2}" appendQueryString="false" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

暂无
暂无

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

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