我想在web.config中对我的产品页面进行自动重写。 所以像这样的“itemdetail.cfm?ProductID = 4399”就变成了“products / 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数据库获取“title”?如果是coldfusion,那么查询将是这样的
<cfquery name="GetData" datasource="myaccessdns">
SELECT Title
FROM Products
WHERE ProductID = #ProductID#
</cfquery>
答案 0 :(得分:1)
我认为你想使用urlMappings。
您可以使用它将/products/Ipad_3
的请求映射到/itemdetail.cfm?ProductID=4399
(甚至可以使用您发布的代码来源于重写的请求)。
这是urlMappings的合适指南:A Look at ASP.NET 2.0's URL Mapping
(注意:您不能使用web.config直接查询数据库)
答案 1 :(得分:1)
因为我提到你有正确的想法,你只需要ColdFusion定期在web.config中生成rewriteMap(或者每次更改产品标题或添加新产品时将其绑定以重新生成/删除)
generateRewriteMap.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。