iis7 asp.net mvc webapi rewrite

荒凉一梦 提交于 2019-12-23 22:41:07

问题


I have the same problem as in the referenced post (Rewrite rule for WebAPI fails because of ExtensionlessUrlHandler) and have tried several attempts to workaround the issue with some success. What I am stuck at now is the following:

Background:

  • IIS7, Rewrite Module and ARR (I am a newbie to rewrite & ARR)
  • Site1 hosting my web front using ASP.Net MVC4 (www.foo.com)
  • Site2 hosting my webapi using ASP.Net MVC4 (svc.foo.com)
  • I have full control over the code & site configs for both sites

Goal:

  • rewrite www.foo.com/svc/* to svc.foo.com/api/*

Working:

  • www.foo.com/svc/somecontroller/0 successfully maps to svc.foo.com/api/somecontroller/0

Not Working:

  • www.foo.com/svc/somecontroller?id=0
  • I turned on Failed Request Tracking and it seems the rewrite module is rewritting the url but for some reason the request is never made to svc.foo.com

Details:

  • To workaround the issue in the referenced post I append a ".s" to the end of every call to a webapi (I have full control so I can do this - not ideal but I need this working so okay for now)
  • Without QS: www.foo.com/svc/somecontroller/0 becomes www.foo.com/svc/somecontroller.s
  • With QS: www.foo.com/svc/somecontroller?id=0 becomes www.foo.com/svc/somecontroller?id=0.s
  • I then use rewrite rules to strip the ".s" and make the real call to svc.foo.com/api/somecontroller/0 or svc.foo.com/api/somecontroller?id=0

I can't for the life of me figure out why when the querystring format is used this doesn't work.

Rewrite Rules:

< rule name="Route requests with QS" stopProcessing="true">
    < match url="^svc/(.*)" />
    < conditions>
        < add input="{QUERY_STRING}" pattern="(.*).s" />
    < / conditions>
    < action type="Rewrite" url="http://svc.foo.com/api/{R:1}?{C:1}" appendQueryString="false" logRewrittenUrl="true" />
< / rule>
< rule name="Route the requests without QS" stopProcessing="true"> 
    < match url="^svc/(.*).s" /> 
    < conditions>
        < add input="{CACHE_URL}" pattern="^(https?)://" /> 
    < / conditions> 
    < action type="Rewrite" url="{C:1}://svc.foo.com/api/{R:1}" logRewrittenUrl="true" /> 
< / rule>

Thanks in advance!


回答1:


This post worked for me. ARR/URL Rewriter within a .net Web API application

Adding the ignoreRoutes did the job perfectly.



来源:https://stackoverflow.com/questions/15437131/iis7-asp-net-mvc-webapi-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!