1

I'm having this issue and have searched Google and StackOverflow, but it seems I can't find a solution for it.

I'm having the following routes mapped in Global.asax.cs

 routes.MapRoute(
       "posRoute", // Route name
       "pos/{guid}", // URL with parameters
       new { controller = "Pos", action = "Index", guid = UrlParameter.Optional } // Parameter defaults
 );

 routes.MapRoute(
       "foxRoute", // Route name
       "fox/{guid}", // URL with parameters
       new { controller = "Fox", action = "Index", guid = UrlParameter.Optional } // Parameter defaults
        );

I want to make a link with the HTML helper Actionlink but it keeps returning an empty link.

@Html.ActionLink("Proceed", "device")

returns

<a href="">Proceed</a>


@Html.ActionLink("Proceed", "device", "Fox" , new { guid = "test" })

returns

<a href="" guid="test">Proceed</a>

as the expected result is as follow:

<a href="/fox/index/test">Proceed</a>

or better

<a href="/fox/test">Proceed</a>
2
  • 1
    If you want to target a particular route, using Html.RouteLink instead of ActionLink might be a good option. Commented Sep 14, 2012 at 13:36
  • In my case, I wasn't specifying the area in routeValues. If you're using areas and trying to link to a controller in another area, do @Html.ActionLink("Proceed", "device", "Fox", new { area = "Areaname", guid = "test" }, null). Commented Dec 22, 2016 at 19:39

1 Answer 1

1

try this overload.

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    string controllerName,
    Object routeValues,
    Object htmlAttributes
)

so your code will be

@Html.ActionLink("Proceed", "device", "Fox" , new { guid = "test" },null)

If you want to pass any HTML attributes like CSS class/ ID of element, you can replace the last parameter calue (null in our case) with that.

Also make sure that you have the generic route definition below your specific routes

routes.MapRoute(
            "posRoute", 
            "pos/{guid}", 
            new { controller = "Pos", action = "Index", 
            guid = UrlParameter.Optional } // Parameter defaults
        );

routes.MapRoute(
            "foxRoute", // Route name
            "fox/{guid}", // URL with parameters
            new { controller = "Fox", action = "Index",
            guid = UrlParameter.Optional } // Parameter defaults
        );

routes.MapRoute("Default","{controller}/{action}/{id}",
          new { controller = "Home", action = "Index",
                    id = UrlParameter.Optional })
Sign up to request clarification or add additional context in comments.

3 Comments

This also gives me '<a href="">Proceed</a>'
Do you have the generic route as the last one in global.asax other that these 2 ?
@JurgenStillaert: Cool. Even i added that to my answer. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.