URL重写模块(IIS)不与IIS上托管的OWIN中间件一起使用 [英] URL Rewrite Module (IIS) not working with OWIN Middleware hosted on IIS

查看:627
本文介绍了URL重写模块(IIS)不与IIS上托管的OWIN中间件一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can i use the URL Rewrite module together with OWIN middleware?

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

Having read http://www.asp.net/aspnet/overview/owin-and-katana/owin-middleware-in-the-iis-integrated-pipeline it says I should be able to use OWIN Middleware in the IIS pipeline using the NuGet package:


Microsoft.Owin.Host.SystemWeb

Microsoft.Owin.Host.SystemWeb

以下是我尝试使用的代码:

Here is the code I am trying to use:

Web.config:

Web.config:

< system.webServer>
< rewrite>
< rules>
< rule name =pandainserterpatternSyntax =ECMAScriptstopProcessing =true>
< match url =。* test。*/>
< conditions trackAllCaptures =true>
< add input ={REQUEST_URI}pattern =/(.*?)/(.*)/>
< / conditions>
< action type =Rewriteurl =http:// {HTTP_HOST}:20000 / v1 / {C:2}appendQueryString =false/>
< / rule>
< / rules>
< / rewrite>
< /system.webServer>

Startup.cs(OWIN):

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重写模块,重写我对同一服务器上其他站点的调用。

What i want to do, is use the OWIN middleware, to allow CORS and fix the OPTIONS preflight http call (described here: How to make CORS Authentication in WebAPI 2?) while still having the abbility to use the IIS Rewrite module, to rewrite my calls to other sites on the same server.

我收到此错误,适用于所有来电。当OWIN与IIS重写模块一起使用时:

I am getting this error, for all calls. when using OWIN with the IIS Rewrite module:


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

HTTP Error 404.4 - Not Found The resource you are looking for does not have a handler associated with it.

详细错误信息:

模块IIS Web核心

Module IIS Web Core

通知MapRequestHandler

Notification MapRequestHandler

处理程序ExtensionlessUrl-Integrated-4.0

Handler ExtensionlessUrl-Integrated-4.0

错误代码0x8007007b

Error Code 0x8007007b

似乎Rewrite模块没有注册,我不确定我做错了什么,或者我是否使用正确的做法?

It seems that the Rewrite module is not registered, and i am not sure what i am doing wrong, or if i am even using the right approach to this?

推荐答案

首先,似乎 IIS URL重写模块默认情况下无法在IIS Express上运行(我还没有尝试在本地开发机器上安装它。)

First off, it seems that the IIS URL Rewrite module is not working on IIS Express per default (i have not tried installing it on a local dev machine).

因此,在使用IIS的Windows Server 2012上运行此功能时并且安装了IIS URL Rewrite模块,我可以看到我的HTTP请求开始被重写。

So when running this on a Windows Server 2012, with IIS and the IIS URL Rewrite module installed i could see that my HTTP(s) requests starting getting rewritten.

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

For further reference I am posting the code I used to fix the CORS Preflight issues, while also having the possiblity to rewrite the HTTP(s) requests to other servers. Please note that this will allow CORS requests from any origin.

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 class:

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 class:

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>

这篇关于URL重写模块(IIS)不与IIS上托管的OWIN中间件一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆