0

We are dealing with a situation similar to these 404 Refresh scenarios We're in html 5 mode, and so we need rewriting to happen to fix the urls. The problem is, solutions that work for the deployed app, do not work in Visual Studio during development.

Our solution for the deployed application works by putting the following in the web.config:

<rewrite>
  <rules>
    <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}" pattern="(api)" negate="true" />
        <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />

      </conditions>
      <action type="Rewrite" url="/myAppName/" />
    </rule>
  </rules>
</rewrite>

And then the following change for index.html:

<base href="/myAppName/">

This mostly works great except for links we have built in Angular. For example

<a href="/partDetails/"></a>

The deployed app will not put "myAppName" in the url. Why is this?

The second issue is that in our Visual Studio dev environment the <base href="/myAppName/"> breaks the application. This throws the app into generating an endless string of errors in the output that look like this:

SourceMap http://localhost:40288/app.module.js.map read failed: Unexpected character encountered while parsing value: <. Path '', line 4, position 1..SourceMap http://localhost:40288/app.core.module.js.map read failed: Unexpected character encountered while parsing value: <. Path '', line 4, position 1..SourceMap http://localhost:40288/app.config.js.map read failed: Unexpected character encountered while parsing value: <. Path '', line 4, position 1..

Exception was thrown at line 1247, column 4 in eval code
0x800a139e - JavaScript runtime error: SyntaxError
Exception was thrown at line 1265, column 4 in eval code
0x800a139e - JavaScript runtime error: SyntaxError

This seems to be an infinite loop until memory runs out. But if I remove the app name from <base href="/myAppName/">, it seems to work fine, but then breaks on the server.

Any ideas where the problem is here? Why are solutions only working on one side, development or deployed, and how can we fix that?

Community
  • 1
  • 1
jlembke
  • 13,217
  • 11
  • 42
  • 56

1 Answers1

1

If you create an absolute URL for the <base> directive, it should work.

For example, with an asp:Literal named "baseLink" in the <head> section:

Option Infer On

Protected Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
    Dim baseUrl = Request.Url.GetLeftPart(UriPartial.Authority) & VirtualPathUtility.ToAbsolute("~/") & "myAppName/"
    baseLink.Text = $"<base href=""{baseUrl}"">"
End Sub

(I just tacked the & "myAppName/" on the end - I'm not at a computer where I can confirm if it should use ....ToAbsolute("~/myAppName/") instead.)

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • We tried that, but it creates the same problem locally of all the Exceptions noted above. – jlembke Feb 21 '17 at 20:35
  • Have you inpected the rendered base URL to make sure it is correct? We can't see it from here. – Andrew Morton Feb 21 '17 at 20:37
  • The code you suggested renders "http://localhost:40288/myAppName/", which would be correct. But so would "/myAppName/". Both of those cause the application to produce the exceptions above and the page never renders. It just infinitely produces those exceptions. If I change base to "/" everything works fine locally, but breaks on the server. – jlembke Feb 21 '17 at 22:03
  • Ahh, you might find it easier to develop with IIS locally. – Andrew Morton Feb 21 '17 at 22:08
  • Thanks Andrew. We ended up using the configuration file to produce the right segment for the link between dev and production environments. I feel dirty, but I blame IIS. :) – jlembke Feb 22 '17 at 19:58
  • 1
    @jlembke I assumed from the ":40288" that you were using IIS Express for development, which has the advantage of not needing to run VS as an administrator. But I use IIS because (a) I can and (b) that's the environment I'm deploying to. Aha! I just found [Disadvantages of Using IIS Express](https://msdn.microsoft.com/en-us/library/58wxa9w5(v=vs.120).aspx) "If your code references root-level resources such as ... without specifying that they are at the project root level, the application might work correctly when you test in Visual Studio but fail when you deploy it to IIS in production." – Andrew Morton Feb 22 '17 at 20:16