2

When I run my project on my machine, it runs on http://localhost:53998/ but when I deploy it to http://test.myserver.com/MyApp/ all links break. I'm using the relative path tilde (~), so a navigation link would be something like:

<a href="~/SomeCtrl/Index">Some Action</a>

On localhost, this works fine (when root is /), but when I deploy my project under /MyApp/ it links the action to http://test.myserver.com/SomeCtrl/Index instead of http://test.myserver.com/MyApp/SomeCtrl/Index so I always get a 404.

Isn't this what the tilde (~) should take care of? Am I doing something wrong here?

EDIT:

This works correctly:

@Html.ActionLink("Some Action", "Index", "SomeCtrl")

And this:

<a href="@Url.Action("Index", "SomeCtrl")">Some Action</a>
Matthew
  • 947
  • 2
  • 13
  • 20
Gaui
  • 8,723
  • 16
  • 64
  • 91
  • Have you tried using `@Url.Action("ActionName", "ControllerName")`? e.g. `@Url.Action("Index", "SomeCtrl")` – Charleh Mar 17 '14 at 11:01
  • You can also use the `@Html.ActionLink()` extension method with the same parameters (plus the 'link text') which will produce the HTML for the link for you – Charleh Mar 17 '14 at 11:02
  • Both `@Url.Action` and `@Html.ActionLink` work correctly, but what I would like to know is why the tilda (`~`) doesn't work? – Gaui Mar 17 '14 at 11:05
  • 1
    No idea, but there may be a clue here: http://stackoverflow.com/questions/4563235/where-does-asp-net-virtual-path-to-resolving-the-tilde - looks like it uses `Request.ApplicationPath` - maybe it's picking up the wrong path - or maybe you have defined your application a level higher in IIS than the site root you want – Charleh Mar 17 '14 at 11:09
  • Makes no sense. ~ and @Html.ActionLink and @Url.Action should all use `HttpRuntime.AppDomainAppVirtualPath` which would be `/MyApp/` – Gaui Mar 17 '14 at 11:14
  • I don't see that in the code on the linked SO question - they must be doing something different or you wouldn't get an issue. – Charleh Mar 17 '14 at 11:16
  • I'll keep this question open because this is an odd behavior. – Gaui Mar 17 '14 at 11:19
  • 1
    You cannot use the tilda in the anything but code that is processed by the framework. As you have seen "Some Action" does not work. http://stackoverflow.com/questions/3077558/use-of-tilde-in-asp-net-path – Ken Brittain Aug 05 '14 at 11:49
  • 4
    @KenBrittain That's not true, MVC4 and above will process `~` correctly into relative urls. The question you link to is from before that time. – jamesSampica Sep 13 '14 at 05:23
  • Make sure you're using Razor 2 and not Razor 1 – Ed DeGagne Oct 27 '14 at 14:27

1 Answers1

0

I had a similar problem, the answer in my case was I had some URL Rewrite settings which were changing the tilde path to point to a different folder. I had to sort out these rewrite rules and it fixed my problem.

Rob Sedgwick
  • 4,342
  • 6
  • 50
  • 87