How to tell IIS7 to redirect traffic to asp.net
I have a url rewrite routine in global.asax
, when the user types:
http://example.com/products/category/electronics
the server serves:
http://example.com/products.aspx?category=45
the problem is that IIS7 does not send this request to be handled by asp.net because the requested path has no folder in the directory structure.
How can开发者_JS百科 I tell IIS to let this request be handled by asp.net without creating a directory for each product and category?
Technically you don't have IIS do this, you use URL Re-Writing to do it. This is a normal feature of ASP.NET MVC. However, it's possible in "classic" form-based asp.net as well.
There's an article about it here: http://msdn.microsoft.com/en-us/library/ms972974.aspx
Listed in the article are a number of ways to accomplish this, as well as a detailed explanation of writing an httpModule.
As a matter of fact, the article shows how to do EXACTLY what you're asking. Search on the page for "when a user visits /Products/Beverages.aspx" on the page and it'll take you right to the demonstration of how it works. (Instructions are higher up in the article.)
In a nutshell don't do this in your Global.asax
. Use the IIS7 Url Rewriter instead:
Using the URL Rewrite Module
You have one of two solutions -
Use a URL Rewrite Map and map every category name to a category ID:
<rewrite> <rewriteMaps> <rewriteMap name="StaticRewrites"> <add key="/products/category/electronics" value="/products.aspx?category=45" /> <add key="/products/category/books" value="/products.aspx?category=46" /> <add key="/products/category/dvds" value="/products.aspx?category=47" /> </rewriteMap> </rewriteMaps> <rules> <rule name="Rewrite Rule 1 for StaticRewrites" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{StaticRewrites:{REQUEST_URI}}" pattern="(.+)" /> </conditions> <action type="Rewrite" url="{C:1}" appendQueryString="False"/> </rule> </rules> </rewrite>
Use a more "dynamic" rewrite:
<rewrite> <rules> <rule name="RedirectUserFriendlyURL1" stopProcessing="true"> <match url="^products\.aspx$" /> <conditions> <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" /> <add input="{QUERY_STRING}" pattern="^([^=&]+)=([^=&]+)$" /> </conditions> <action type="Redirect" url="products/{C:1}/{C:2}" appendQueryString="false" /> </rule> <rule name="RewriteUserFriendlyURL1" stopProcessing="true"> <match url="^products/([^/]+)/([^/]+)/?$" /> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="products.aspx?{R:1}={R:2}" /> </rule> </rules> </rewrite>
In the above case you'd need to change your
products.aspx
page to accept the category name, then in that page look up the actual integer ID of the category in the database.
Passing the rewrite responsibility to IIS7's URL Rewrite module (or any other rewriter such as Iconics IIRF or HeliconTech's ISAPI Rewrite) means that you can change these rules separately rather than burning them into your code. Burning these rules into your code means you have to redeploy every time you make a change.
精彩评论