0

I am new to asp.net and currently learning url routing

In facebook link(https://www.facebook.com/james.wood) how does james.wood work?

I tried doing the same thing in my asp.net by using this code added in my global

route.MapPageRoute("Profile", "epubtest/profile/{profileid}", "~/epubtest/Profile.aspx");

But I cant make it work with dot like james.wood

Does the dot means another parameter or just one single parameter?

It works without a dot on it just one single word

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63

1 Answers1

0

1) You can add this route:

routes.MapRoute(
    name: "User",
    url: "{username}",
    defaults: new { controller = "Destiny", action = "Index" },
    constraints: new { username = new UserNameConstraint() }
);

2) Create this class:

public class UserNameConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        List<string> users = new List<string>() { "username1", "username2" };

        var username = values["username"].ToString().ToLower();

        return users.Any(x => x.ToLower() == username);
    }
}

3) DestinyController

public class DestinyController : Controller
{
    public ActionResult Index(string username)
    {
        return View();
    }
}

I hope I've helped. Hugs!

Rafael Botelho
  • 398
  • 5
  • 11