Icemanind Icemanind - 1 year ago 74
C# Question

Redirecting to a custom route in MVC

I have setup a custom route in my MVC site. I am having trouble figuring out how to redirect to it though. Here is my custom route code:

public class MemberUrlConstraint : IRouteConstraint
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values,
RouteDirection routeDirection)
if (values[parameterName] != null)
var permalink = values[parameterName].ToString();

return string.Equals(permalink, Member.GetAuthenticatedMembersUrl(),
return false;

Here is my

public class RouteConfig
public static void RegisterRoutes(RouteCollection routes)

name: "MembersRoute",
url: "{*permalink}",
defaults: new { controller = "Dashboard", action = "Index" },
constraints: new { permalink = new MemberUrlConstraint() });

name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

This is working fine. If I put something like
in my browser URL, it works fine.

My question is, how do I programmically redirect to this page? I've tried this:

RedirectToAction("Index", "Dashboard"); // Goes to

RedirectToRoute("MembersRoute", new {controller = Infrastructure.Member.GetAuthenticatedMembersUrl(), action = "Index"}); // Goes to

I want to be able to redirect to

Answer Source


RedirectToAction("Index", "Dashboard", new { permalink = "Joes-Page"});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download