如何将URL Rewrite模块与OWIN中间件一起使用?
阅读http://www.asp.net/aspnet/overview/owin-and-katana/owin-middleware-in-the-iis-integrated-pipeline后,我说我应该可以使用NuGet包在IIS管道中使用OWIN中间件:
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调用(此处描述:How to make CORS Authentication in WebAPI 2?),同时仍然无法使用IIS重写模块,重写我的调用同一台服务器上的其他站点。
对于所有来电,我收到此错误。将OWIN与IIS重写模块一起使用时:
HTTP错误404.4 - 未找到 您正在寻找的资源没有与之关联的处理程序。
详细错误信息:
模块IIS Web核心
通知MapRequestHandler
Handler ExtensionlessUrl-Integrated-4.0
错误代码0x8007007b
似乎Rewrite模块没有注册,我不确定我做错了什么,或者我是否使用了正确的方法?
答案 0 :(得分:0)
首先,似乎IIS URL Rewrite module默认情况下不支持IIS Express(我没有尝试在本地开发机器上安装它)。
因此,当在Windows Server 2012上运行此程序时,如果安装了IIS和IIS URL重写模块,我可以看到我的HTTP请求开始被重写。
为了进一步参考,我发布了用于修复CORS Preflight问题的代码,同时还有可能将HTTP(s)请求重写到其他服务器。请注意,这将允许来自任何来源的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>