refer:http://www.improve.dk/blog/2006/12/11/making-url-rewriting-on-iis7-work-like-iis6
Upgrading to IIS 7 should be rather transparent, unfortunately that is
not the case when it comes to URL rewriting as we knew it from IIS 6.
In IIS 6 all we had to do was to add a wildcard mapping making sure
that all requests went through the ASPNET ISAPI process. After this was
done, one could create a global.asax file that would either pass
requests directly through or rewrite the URL based on an internal
algorithm.
UPDATE: Please see my updated post
on how to do proper URL rewriting using IIS 7.
I
didn't really expect this to be a problem when I first requested
http://localhost for the first time after setting up my site on IIS 7
(all default settings).
Unfortunately
this was what I was presented with. Anyone having worked with wildcard
mappings from IIS 6 will recognize this, this is the result you'll get
after adding a wildcard mapping without having created your URL
rewriting functionality. After adding a wildcard map the IIS will not
automatically find a default file (by design).
This however, is
not the problem cause here. I already have my URL rewriting
functionality written in my global.asax BeginRequest method and I'd
really like to just reuse my current code. Although the new IIS has a
whole new bunch of features - one of them being a new "more correct"
way of doing URL rewriting -, I really just wan't to get my website up
and running again so I can continue my work.
What I present
below is a quick'n'dirty hack that will get my old URL rewriting code
to work again. It may not be the IIS 7 way of doing it, and it may not
work in your case, it depends on the type of URL mapping you're doing
in your project. In short, YMMV
.
My scenario
For
this website, improve.dk, all URL's except static files are requested
as though they were folders. That means you will not see any pages
ending in anything but a /. Any static files are requested as usual.
That means I can be sure that a regular expression like *.* will catch
all static files, while * will catch all pages - as well as the static
files!
How I got URL rewriting to work like IIS 6
Start by opening the IIS Manager and selecting your website.
Now enter the "Handler Mappings" section:
Notice
the "StaticFile" handler. Currently it's set to match * and catch both
File and Directory requests. If you look back at the first image,
you'll notice that the error message details that the handler causing
the 404 error is the StaticFile handler. As I know that all my static
files will have a file extension (also I don't care for directory
browsing), I'll simply change my StaticFile handler so it only matches
*.* - and only files.
Your StaticFile handler should now look like this:
Now,
if you go back and make a request to http://localhost you'll still get
the 404 error, but this time the request is not handled by the
StaticFile handler, actually it doesn't get handled by any handler at
all:
What
needs to be done now is that we need to map any and all requests to the
aspnet_isapi.dll isapi file - just like we would usually do in IIS 6.
Add a new Script Map to the list of Handler Mappings and set it up like this:
Click OK and click Yes
at the confirmation dialog:
Now if you make a request to either http://localhost or any other file you'll get the following error:
Looking throug the Event log reveals the cause of the error:
The
aspnet_isapi.dll file cannot be used as a Handler for websites running
in the new IIS 7 Integrated Mode, thus we will need to make our website
run in classic .NET mode. Right click your website node in the IIS
Manager and select Advanced Settings. Select the "Classic .NET AppPool"
and close the dialog boxes:
Now you should be able to make a request to http://localhost and see it work. Your URL rewriting should work as a charm aswell:
But obviously somethings wrong. Making a request to any static file will reveal the problem:
"Failed
to Execute URL", what a great descriptive error. Fortunately you won't
have to spend hours ripping out hair... As I have already done that -
at least I'll save a trip or two to the barber.
The problem is
that the static files are being processed by the aspnet_isapi.dll file
instead of simply sending the request along to the StaticFile handler.
If you click the "View Ordered List..." link in the IIS Manager from
the Handler Mappings view, you'll see the order in which the handlers
are being executed for each request:
When
you add a new Script Map it'll automatically get placed at the very top
of the line taking precedence over any other handlers, including the
StaticFile one.
What we have to do is to move our Wildcard
handler to the very bottom, below the StaticFile handler. By letting
the StaticFile handler have precedence over our Wildcard handler we
ensure that any static files (matching *.*) gets processed correctly
while any other URL's gets passed along to our own Wildcard handler
that'll do the URL rewriting and make business work as usual:
After doing so, both static files as well as your custom URL's should execute as they would under IIS 6:
Disclaimer
Please
notice that this is a hack. This is not the way URL rewriting is
supposed to be done under IIS 7. But instead of spending hours upon
hours investigating how to do it the right way, this is quick fix to
make things work like they did before.
Also please note that
this solution is intended to work for my specific situation. Your needs
for URL rewriting may not necessarily match mine, so you may have to
modify certain settings to suit your specific needs.
分享到:
相关推荐
URLRewriting 页面静态化.实现表态化用的,下载后直安装后直接用就OK了.
URL Rewriting in ASP.NET1.1 C#
URLRewriting 重定向 URLRewriting 重定向
在分析原理之前,我们先来做一个例子测试一下(IIS URL Rewrite模块需要IIS7的支持)。 1.为Customer/1的URL建立对应的MVC程序 首先建立一个普通的MVC3程序,建立一个简单的CustomerController以及一个简单的Detail...
分析如何使用微软提供的ASP.NET来对动态产生的URL地址进行网址重写。 网址重写是实现一种截取网址请求并将其进行处理后重新指向到一个指定的网址的过程。作者本人在对各种实现网址重写的技术进行研究和探讨后得出的...
URL Rewriting in ASP.NET
URLRewriting 重写 使用说明。如果你不想让别人看到你动态网页的直接路径时,就用它。
Programmatically Setting Control Adapters for URL Rewriting and AJAX。
分析如何使用微软提供的ASP.NET来对动态产生的URL地址进行网址重写。 网址重写是实现一种截取网址请求并将其进行处理后重新指向到一个指定的网址的过程。作者本人在对各种实现网址重写的技术进行研究和探讨后得出的...
尝试依葫芦画瓢写了自己的一个UrlRewriting。本人资源分比较低,就希望各位支持了。 一、参数 1、Enabled:bool值,设置是否应用Url重写功能; 2、IsUseCache:bool,设置是否启用Cache功能;(写该功能...
看 看 吧 ,很 不 错 的 东 东 !
在 ASP.NET 中执行 URL 重写
分析如何使用微软提供的ASP.NET来对动态产生的URL地址进行网址重写。 网址重写是实现一种截取网址请求并将其进行处理后重新指向到一个指定的网址的过程。作者本人在对各种实现网址重写的技术进行研究和探讨后得出的...
分析如何使用微软提供的ASP.NET来对动态产生的URL地址进行网址重写。 网址重写是实现一种截取网址请求并将其进行处理后重新指向到一个指定的网址的过程。作者本人在对各种实现网址重写的技术进行研究和探讨后得出的...
分析如何使用微软提供的ASP.NET来对动态产生的URL地址进行网址重写。 网址重写是实现一种截取网址请求并将其进行处理后重新指向到一个指定的网址的过程。作者本人在对各种实现网址重写的技术进行研究和探讨后得出的...
分析如何使用微软提供的ASP.NET来对动态产生的URL地址进行网址重写。 网址重写是实现一种截取网址请求并将其进行处理后重新指向到一个指定的网址的过程。作者本人在对各种实现网址重写的技术进行研究和探讨后得出的...
DotBBS 开源论坛 由ASP.NET+Access架构而成,当然也可以和MSSQL搭配,程序中使用有第三方的组件,像CuteEditor以及URL重写组件UrlRewriting.dll等。这个论坛界面挺清新,如上图示,功能可以满足中小型网站使用,Ajax...
UrlRewriting Filter 是一个 servlet 过滤器,它自动将 request.encodeURL() 应用于 servlet 响应正文中的 url。 这允许用户不必担心手动将 request.encodeURL() 应用于这些 url 以进行会话传播。