Welche Route würde ich benötigen, um Vanity-URLs bereitzustellen?
Ich möchte meinen Benutzern eine Vanity-URL zur Verfügung stellen, etwa:
www.foo.com/sergio
Welche Route müsste ich erstellen?
Stellen Sie sich vor, ich habe den folgenden Controller und die folgende Aktion. Wie kann ich diesem Controller eine Vanity-URL zuordnen?
public ActionResult Profile(string username)
{
var model = LoadProfile(username);
return View(model);
}
Folgendes habe ich ausprobiert und was passiert:
Option A:Jede URL wird auf dieser Route abgefangen, dh jede von mir eingegebene URL leitet mich nicht nur an den Account Controller weiterfoo.com/[USERNAME]
. Nicht gut.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Profile",
"{username}",
new { controller = "Account", action = "Profile", username = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
Option B:Standardrouten funktionieren gut, aber wenn Sie versuchen, ein Profil zu besuchenfoo.com/[USERNAME]
Ich erhalte einen HTTP 404-Fehler.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"DentistProfile",
"{username}",
new { controller = "Account", action = "Profile", username = UrlParameter.Optional }
);
}