I have this simple User Area in my MVC 4 project.
public class UserAreaRegistration : AreaRegistration
{
public override string AreaName { get { return "User"; } }
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute("User_Constraint",
"{userName}/{controller}/{action}/{id}",
new { userName = string.Empty, controller = "Products", action = "Index", id = UrlParameter.Optional },
new { userName = new UserNameRouteConstraint() },
new[] { "T2b.Web.Areas.User.Controllers" }
);
}
}
To make sure the User Name exists I have a RouteConstraint called UserNameRouteConstraint()
All this does is a simple lookup in my users table and return true if the user has been found.
So far so good, this construction works fine!
Now; My view in the User Area has the following line of code
@Html.ActionLink("More information", "details", new {id = product.Guid})
This single line causes the UserNameRouteConstraint() to be called….
How and why!? If I write the link in plain HTML (see example below) it works well, but I want to keep to the MVC Principles as close as possible.
<a href="/username/Products/details/@product.Guid">More information</a>
Is there any way to prevent the RouteConstraint call?
Whenever routes are generated the constraints are processed.
You can add this check to stop the constraint depending on whether the constraint is handling an incoming request or generating a URL from a function like
ActionLink: