Cannot load signalr hubs file correctly after IIS

2019-09-12 07:20发布

问题:

I'm working on an application using ASP.NET Web API, SignalR and AngularJS. Ui-router is being used and html5 Mode is enabled.

I've edited the web.config file and the rewrite rule is like this:

<rule name="AngularJS Routes" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_URI}" matchType="Pattern" pattern="api/(.*)" negate="true" />
            <add input="{REQUEST_URI}" matchType="Pattern" pattern="signalr/(.*)" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
</rule>

Everything works great in visualstudio localhost and the auto-generated signalr hubs file is loaded successfully.

The problem is when I deployed the application on server (windows server 2008 R2), and run the application, <script src="signalr/hubs"></script> the request to signalr hubs file keep sending me index.html back as response, which I think the reason may be this line of code <add input="{REQUEST_URI}" matchType="Pattern" pattern="signalr/(.*)" negate="true" /> is not working correctly so the request is being rewrite to the root url.

I've tried to change the pattern to "~/signalr/(.*)", "~/(signalr)" or "~/signalr but none of these work.

Any suggestion? Thank in advance.

回答1:

Use the following, that should do the trick:

<add input="{REQUEST_URI}" pattern="^/(signalr)" negate="true" />