Saturday, March 27, 2021

Blazor ignore rewrite rule

The root folder of a Blazor app contains a Web.config file with the following rewrite rule to send all requests into the Webassembly runtime:
<rewrite>
  <rules>
    <rule name="Serve subdir">
      <match url=".*" />
      <action type="Rewrite" url="wwwroot\{R:0}" />
    </rule>
    :
  </rules>
</rewrite>

Note that it intercepts every request without exception. But you may want exceptions, like my case where the Blazor app was deployed into the root of a web site which had apps in existing virtual directories. The Blazor rule was "hiding" the virtual directories so all requests for them produced the standard Blazor "nothing at this address" message.

To make a path exempt from Blazor routing you can insert a rule like the following before the one above:

    <rule name="Ignore myapp" stopProcessing="true">
      <match url="myapp" />
      <action type="None" />
    </rule>

Now the request for path myapp will be ignored and processed normally without reaching Blazor.

Hours of web searches failed to reveal the trick above, so I'm wondering if I'm the only person in the world who hit this routing problem. After reading the MSDN documentation on the rewrite module I eventually stumbled upon the correct rule. You can combine multiple paths into one large rule using the <condition> element, which is explained in the documentation.

One remaining problem is how to deploy a Blazor app with the Web.config already edited to contain the extra ignore rule. Another hour of searching fails to reveal an answer. It seems that a Blazor deploy will overwrite any existing Web.config and I can't find any pre-deploy point where the file can be adjusted.

No comments:

Post a Comment