我想慢慢将Node.js应用程序转换为ASP.NET WebAPI 2.0.我目前正在使用IIS并坚持使用IIS.所以,我想将它们托管在同一台服务器上,但将一些URI引导到新平台. 我将如何在web.config中执行此操作?
我将如何在web.config中执行此操作? node.js的当前web.config如下所示:
<configuration>
<system.webServer>
<handlers>
<!-- indicates that the app.js file is a node.js application
to be handled by the iisnode module -->
<add name="iisnode" path="beta/app.js" verb="*" modules="iisnode" />
</handlers>
<rewrite>
<rules>
<!-- Don't interfere with requests for node-inspector debugging -->
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^beta/app.js\/debug[\/]?" />
</rule>
<!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
<rule name="StaticContent">
<action type="Rewrite" url="beta/public{REQUEST_URI}" />
</rule>
<!-- All other URLs are mapped to the Node.js application entry point -->
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True" />
</conditions>
<action type="Rewrite" url="beta/app.js" />
</rule>
</rules>
</rewrite>
<httpErrors errorMode="Detailed"/>
</system.webServer>
</configuration>
文件结构是:
- web.config (the one shown above)
-> node
- app.js
- ...
-> webapi
- web.config
- global.asax
- ...
我在想我应该编写一个新规则,列出要转到WebAPI的URI.但是,我不太清楚该怎么做.我的猜测是我会为每个带有input属性的URI添加一个条件.我也在想我应该指向ASP.NET WebAPI项目,但是我更加无能为力,因为Node.js我只是指向app.js文件.
好的,这就是我最终要做的事情.实际上非常直接.但是当你不熟悉IIS时,它可能是令人生畏的.我将原始web.config放入节点目录中.我认为如果不这样做,iisnode处理程序会干扰WebAPI配置.因此,节点目录中的新node.js web.config将如下所示:
<configuration>
<system.webServer>
<handlers>
<!-- indicates that the app.js file is a node.js application
to be handled by the iisnode module -->
<add name="iisnode" path="app.js" verb="*" modules="iisnode" />
</handlers>
<rewrite>
<rules>
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^app.js\/debug[\/]?" />
</rule>
</rules>
</rewrite>
<httpErrors errorMode="Detailed"/>
</system.webServer>
</configuration>
对于root web.config我直接指向静态文件,绕过node.js.这意味着我将不得不编写一些自定义代码来处理gzip压缩文件的重写 – 我稍后会详细说明.我还为每个重写规则添加了属性stopProcessing.这也搞乱了代码,因为它实际上不会重写我想要的地方,因为重写会被覆盖.请注意,接受版本控制标头尚未实际测试过 – 我没有任何理由相信它不会起作用.默认情况下,最后一次重写将所有uris指向webapi应用程序.
在WebAPI项目中,我必须将所有路由路由到webapi / api,因为它不在根文件夹中.在我从node.js迁移所有内容之后,我可能会将webapi目录设置为项目的根文件夹,因此它不再需要我的路由中的webapi.但这一切都隐藏在客户端之外.
所以这是实际的代码:
<configuration>
<system.webServer>
<rewrite>
<rules>
<!-- test item for webapi folder -->
<rule name="StaticContent2" stopProcessing="true" >
<conditions>
<add input="{REQUEST_URI}" pattern="^/def" />
</conditions>
<action type="Rewrite" url="webapi{REQUEST_URI}" />
</rule>
<!-- rewrite static items which exist on node -->
<rule name="Node Static" stopProcessing="true" >
<conditions>
<add input="{REQUEST_URI}" pattern=".*\.[A-Za-z2]{2,5}$" />
</conditions>
<action type="Rewrite" url="node/public{REQUEST_URI}" />
</rule>
<rule name="WebAPI Version 2" stopProcessing="true">
<conditions>
<add
input="{HEADER_ACCEPT}"
pattern="vnd.fieldops.v2"
ignoreCase="true"
/>
</conditions>
<action type="Rewrite" url="webapi{REQUEST_URI}" />
</rule>
<!-- rewrite to node for dynamic items -->
<rule name="Node Dynamic" stopProcessing="true" >
<conditions>
<add
input="{REQUEST_URI}"
pattern="^/api/(dealerservicereports|chat|dealers|dealerequipment|dealercloseout|publications|tokens|users|\?)"
ignoreCase="true"
/>
</conditions>
<action type="Rewrite" url="node/app.js" />
</rule>
<!-- rewrite everything else to webapi -->
<rule name="WebAPI Dynamic" stopProcessing="true" >
<action type="Rewrite" url="webapi{REQUEST_URI}" />
</rule>
</rules>
</rewrite>
<httpErrors errorMode="Detailed"/>
</system.webServer>
</configuration>
