我想在web.config中为我的产品页面进行自动重写。所以像这样的"项目细节.cfm?产品 ID=4399"变为"产品/Ipad_3"。我得到了这个工作
<?xml version="1.0"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite for Products" stopProcessing="true">
<match url="products/(.+)" />
<action type="Rewrite" url="itemdetail.cfm?ProductID={Products:{R:1}}" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="Products">
<add key="Ipad_3" value="4399" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer>
</configuration>
如何修改web.config中的代码,使其使用"ProductID"从MS Access数据库获取"标题"?如果是冷聚变,查询将是这样的
<cfquery name="GetData" datasource="myaccessdns">
SELECT Title
FROM Products
WHERE ProductID = #ProductID#
</cfquery>
是的,正如我提到的,您的想法是正确的,您只需要 ColdFusion 定期在 web.config 中生成重写映射(或者将其绑定为每次更改产品标题或添加新产品时重新生成)
生成重写地图.cfm
<cfquery name="AllProducts" datasource="#request.dsn#">
select ProductID, URLTitle
from Products
</cfquery>
<cfsavecontent variable="WebConfigFileData"><?xml version="1.0"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite for Products" stopProcessing="true">
<match url="products/(.+)" />
<action type="Rewrite" url="itemdetail.cfm?ProductID={Products:{R:1}}" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="Products">
<cfoutput query="allProducts"><add key="#URLTitle#" value="#ProductID#" />
</cfoutput>
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer>
</configuration></cfsavecontent>
<!--- Backup the Original Web.Config --->
<cffile
action = "copy"
source = "c:/inetpub/wwwroot/mysite/web.config"
destination = "c:/inetpub/wwwroot/mysite/web.config.#getTickCount()#">
<cffile
action = "write"
file = "c:/inetpub/wwwroot/mysite/web.config"
output = "#WebConfigFileData#">
我这样做的另一种方法是转换/products.index.cfm 在/products/This_Products_Title 之后搜索标题,解析出过去的所有内容/products/然后对产品标题进行数据库搜索并拉出相应的页面。 任何未找到的内容,都会将用户返回到主页。这确实要求您更改应用程序中的所有链接以使用 FriendlyURLTitle。
我想你想使用urlMappings。
您可以使用它将/products/Ipad_3
请求映射到/itemdetail.cfm?ProductID=4399
(甚至可能源自使用您发布的代码重写的请求)。
这是urlMappings的一个不错的指南:看看 ASP.NET 2.0的URL映射
(注意:不能使用 web.config 直接查询数据库)