Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML.ActionLink() generating incorrect URLs

I'm having difficulty getting HTML.ActionLink() create correct links.

When I am at http://localhost:49242/

My options are:

  • http://localhost:49242/en-us/PinnedSites ,
  • http://localhost:49242/en-us/addons ,
  • http://localhost:49242/en-us/trackingprotectionlists

But when I navigate to http://localhost:49242/en-us/PinnedSites my options are:

  • http://localhost:49242/en-us/PinnedSites/PinnedSites ,
  • http://localhost:49242/en-us/PinnedSites/addons ,
  • http://localhost:49242/en-us/PinnedSites/trackingprotectionlists

All of these urls fail, of course.

Here's my html:

.cshtml (Html / razor)

<li id="nav_pinning">
    @Html.ActionLink( "pinned sites", "index", "PinnedSites")</li>
<li id="nav_addons">
    @Html.ActionLink("add-ons", "index", "addons")</li>
<li id="nav_control">
    @Html.ActionLink("tracking protection lists", 
                    "index", 
                    "trackingprotectionlists",
                    null,
                    null)</li>

Global.asax.cs

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("favicon.ico");
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            "Default", // Route name
            "{culture}/{controller}/{action}", 
            new { culture = "en-us", 
                controller = "Home", 
                action = "Index", 
            }
        );
    }

Where is my problem? Is it possible that /PinnedSites/ is not actually going to the PinnedSitesController?

(And I will be actively watching this, please comment if you want me to try things or provide more code.)

like image 513
Civilian Avatar asked Jun 30 '26 18:06

Civilian


1 Answers

Do you have a HomeController as part of your project? If so you should register the default controller along the lines of:

 routes.MapRoute(
         "Default", // Route name
         "{controller}/{action}/{id}", // URL with parameters
         new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );

However, that aside have you tried simply making the Route directly to your PinnedSites controller?

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("favicon.ico");
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            "Default", // Route name
            "{culture}/{controller}/{action}", 
            new { culture = "en-us", 
                controller = "PinnedSites", 
                action = "Index", 
            }
        );
    }
like image 79
Jesse Avatar answered Jul 02 '26 07:07

Jesse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!