Killer Whale
Killer Whale

Reputation: 87

ASP.NET MVC 2 - How to write RouteUrl with Url property in context.MapRoute modified?

I have an area named User, and I write MapRoute for it:

  context.MapRoute(
      "User_Category",
      "User/Category/{categoryId}",
       new { controller = "Product", action = "Category", categoryId = UrlParameter.Optional },
       new { categoryId = @"\d+" }
  );

This is other example, I have a link:

<%=Html.ActionLink("Điện thoại", "Category", new { area = "User", controller = "Product", id = 1  }, null) %>
(http://localhost:8578/User/Product/Category/1)

Sure, I can't do this:

<%=Html.ActionLink("Điện thoại", "User/Category", new { area = "User", controller = "Product", id = 1  }, null) %>

Following MapRoute above, it's modified. It means that it's in an Area, I don't know how to pass Area Name into ActionLink to have: http://localhost:8587/User/Category/1

But the thing I want is replace ActionLink to RouteUrl to get absolute link like **http://localhost:8587/User/Category/1**

What should I do? And how I can remove User Name in Url? Thanks for watching!

Upvotes: 0

Views: 485

Answers (1)

Ian Routledge
Ian Routledge

Reputation: 4042

I think it is because your route is defined with the route parameter categoryId but your action link uses the parameter just id? If so, try this instead:

<%=Html.ActionLink("Điện thoại", "Category", new { area = "User", controller = "Product", categoryId= 1  }, null) %>

If you want the full absolute URL then you could do:

<a href="<%= Html.ViewContext.HttpContext.Request.Url.GetLeftPart(UriPartial.Authority) + Url.Action("Category", new { area = "User", controller = "Product", categoryId= 1  }) %>">Điện thoại</a>

Upvotes: 1

Related Questions