-1

I can't find out how to solve this. I have two URLs. These are /my-url-1 and /my-url-2. Both going to different views.

The thing is that I have an ActionLink on /my-url-1's view which should make /my-url-2 and go to that view.

The problem is that ActionLink makes /my-url-1/my-url-2 as the URL and not just /my-url-2.

I was searching two days about how to fix it but couldn't find anything.

PD: I'm not using Areas so please don't tell me that I just should put the "area" parameter as a "".

These are two urls which goes to different controllers and different actions.

View which has the ActionLink (URL:/my-url-1) :

<div class="btn-index-container">
     @Html.ActionLink("Url 2", "MyAction", "MyController")
</div>

This ActionLink should render:

<a href="/my-url-2">Url 2</a>

But it's rendering:

<a href="/my-url-1/my-url-2">Url 2</a>

where /my-url-1 is my current URL

Route Config

         routes.MapRoute(
            name: "route1",
            url: "my-url-2", //without parameters
            defaults: new { controller = "MyController", action = "MyAction" },
        );

        routes.MapRoute(
            name: "route2",
            url: "my-url-1", //without parameters too
            defaults: new { controller = "MyController2", action = "MyAction2" }
        );

So, when I go to localhost:port/my-url-1 it loads MyAction2 which renders a view. This view has inside an ActionLink(described above) which should render a /my-url-2 link.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
HNL
  • 103
  • 13
  • @Leandrohereñu Since you have not provided any code or detailed explanation of what you are trying to achieve. If you are trying to navigate from one view to other by placing it as a link. You can use LINK-TEXT – Tango Dec 05 '18 at 14:39
  • It should be `@Html.ActionLink("LinkText", "Action", "Controller")`, the controller and action are in the wrong order in your example – haldo Dec 05 '18 at 14:45
  • It isn't that some of us *want* some code, it's that we *cannot help you* without seeing your code. Imagine going to your auto mechanic and not letting him actually inspect your car. –  Dec 05 '18 at 14:45
  • @Haldo you are right! I wrote here in the bad order. Thanks :) – HNL Dec 05 '18 at 14:49
  • Hi @Tango! How are you? I've tried with ActionLink, Url.Action and RouteLink but all take the same problem. Thanks for answer! – HNL Dec 05 '18 at 14:51
  • Sorry @Amy. I thought that the code was no necessary. I thought that it wasn't to difficult to get in mind what was going on. Do you have some advice for it? – HNL Dec 05 '18 at 14:53
  • I'm guessing its because of how your `MyAction` method is configured. Can you show us how routing for that method is configured? –  Dec 05 '18 at 14:53
  • yes of course! I'll add it into the question – HNL Dec 05 '18 at 14:58
  • Something doesn't seem right regarding your routing, but I can't put my finger on it. You might consider using something like https://www.nuget.org/packages/routedebugger/ to get a handle on what is happening under the covers. –  Dec 05 '18 at 15:23
  • @Amy I found a post which was written for you, here: https://stackoverflow.com/questions/34665297/resolving-an-image-uri-relative-to-application-root please, as I can't comment on that post because my reputation, could you update the link for get the MVC code? The new URL is: https://github.com/aspnet/AspNetWebStack/blob/master/src/System.Web.Mvc/ – HNL Dec 05 '18 at 18:41
  • @Leandrohereñu That *looks* like a different issue. Does the solution there fix your problem? If so, props for finding it. –  Dec 05 '18 at 18:42
  • @Leandrohereñu Thank you, I have updated the link and my answer in general. –  Dec 05 '18 at 18:51

2 Answers2

1

Well, I've worked inside the MVC framework and I could told you about how Url.RouteUrl or Html.RouteLink works. At the end, the method which create the URL is GetVirtualPathForArea (this method is called before UrlUtil.GenerateClientUrl, which receive the VirtualPathData.cs created by GetVirtualPathForArea, as a parameter) from System.Web.Routing.RouteCollection.cs.

Here I left a link to the MVC source code: https://github.com/aspnet/AspNetWebStack/blob/master/src/System.Web.Mvc

I found that, my Request.ApplicationPath was changing when I loaded /my-url-1. It was crazy because the application path was /.

At last, the problem was that the /my-url-1 was pointing to a virtual directory created on the IIS some time ago by error. To know where your IIS configuration file is, please follow the link below:

remove virtual directory in IIS Express created in error

The solution was remove the .vs directory (which contains the config .vs\config\applicationhost.config) and rebuild

I think most of the Helpers that render URLs works more or less in the same way, so I hope it'll useful for all of you!

HNL
  • 103
  • 13
0

In your case, maybe no its necesary, just pass the parameters with null values, E.G.

@Html.ActionLink("Español", null, null, new { Lng = "es" }, null)

In this way, the parameters change, and the view is relative, depending on where you are.

Ransomware0
  • 427
  • 3
  • 10