设为首页 收藏本站
查看: 3597|回复: 0

[经验分享] IIS7及其以上配置URLRewriter,出现404错误(模块 IIS Web Core 通知 MapRequestHandler 处理程序 Static

[复制链接]

尚未签到

发表于 2015-8-16 08:46:57 | 显示全部楼层 |阅读模式
  原文:http://improve.dk/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).
DSC0000.jpg
  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.
DSC0001.jpg
  Now enter the "Handler Mappings" section:
DSC0002.jpg
  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.
DSC0003.jpg
DSC0004.jpg
  Your StaticFile handler should now look like this:
DSC0005.jpg
  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:
DSC0006.jpg
  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:
DSC0007.jpg
DSC0008.jpg
  Click OK and click Yes at the confirmation dialog:
DSC0009.jpg
  Now if you make a request to either http://localhost or any other file you'll get the following error:
DSC00010.jpg
  Looking throug the Event log reveals the cause of the error:
DSC00011.jpg
  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:
DSC00012.jpg
  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:
DSC00013.jpg
  But obviously somethings wrong. Making a request to any static file will reveal the problem:
DSC00014.jpg
  "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:
DSC00015.jpg
  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:
DSC00016.jpg
  After doing so, both static files as well as your custom URL's should execute as they would under IIS 6:
DSC00017.jpg
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.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-99572-1-1.html 上篇帖子: ASP.NET 应用程序建在HTTP位置,在IIS下调试ASP.NET应用程序 下篇帖子: 译:重置/还原Windows IIs设置为默认设置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表