How can I override a .svc file in my routing table?

随声附和 提交于 2019-11-30 11:37:39

问题


I have this URL that was used from some JSON post back from the main website:

http://site/Services/api.svc/UpdateItem

We are in the process of updating the web site slowly to ASP.Net MVC 2 and don't want to break any current URL's in the system. (JavaScript refresh issues)

I have deleted the /Services/api.svc and have moved the logic for this API call into the following Controller:

http://site/LegacyApi/UpdateItem

Unfortunately, when adding the route, I can't seem to get it to override the api.svc and keep getting a 404 error.

Route:

    routes.MapRoute(
        "UpdateItemApi",
        "Services/api.svc/UpdateItem",
        new { controller = "LegacyApi", action = "UpdateItem" }
    );

According to the MSDN on this specific issue, the routing should go through.

Any help on this is greatly appreciated.


Update

Routing for standard .aspx pages works as intended, so this seems to be something perticular with the .svc files and how they are handled.


回答1:


The problem you're running into is due to a build provider that is registered for .svc files. This build provider is registered by the default machine level web.config file.

In order to get routing to work in this case, you'll need to remove the build provider in your application's web.config file. The following snippet shows how to remove the .svc extension from the list of build providers.

  <system.web>
    <compilation debug="true" targetFramework="4.0">
        <buildProviders>
            <remove extension=".svc"/>            
        </buildProviders>
    ...



回答2:


Using URL Rewrites I was able to do what this link suggested, only backwards:

  <system.webServer>
    <rewrite>
      <rules>
        <rule name="LegacyApiService" stopProcessing="true">
          <match url="^Services/api.svc/(.*)$" />
          <action type="Rewrite" url="LegacyApi/{R:1}" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>


来源:https://stackoverflow.com/questions/3662555/how-can-i-override-a-svc-file-in-my-routing-table

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