Vanity profile URL in ASP.net

854 views Asked by At

I am creating an application in ASP.net where each user will have their own profile that they can share with others.

the default address for their profile will be:

www.domain.com/User/Profile/[UserID]

when they specify a vanity URL, someone can enter

www.domain.com/User/Profile/[Vanity]

and be directed to the same page. The profile page works with only one function taking in a UserID with the following function in the controller:

    public ActionResult Profile(int id)
    {
        ppUser viewerChoice = DB.User_GetUserByPersonID(id);

        return View(viewerChoice);
    }

However, when I add another that takes a string

    public ActionResult Profile(string vanity)
    {
        ppUser viewerChoice = DB.User_GetUserByVanity(vanity);

        return View(viewerChoice);
    }

This causes an AmbiguousMatch Exception. How would I go about making sure that it calls the correct function?

1

There are 1 answers

4
LukeP On BEST ANSWER

You have few options.

  • You can capture the string parameter only and read it and if it's a number convert it to int.
  • You can configure 2 separate routes and use regular expressions to recognise the parameter and process it accordingly
  • Use separate actions for int and string parameters.

I would be very careful with you approach (same actions, different parameters) as there is a risk of user choosing int (same format as user id) for a vanity URL. How would you resolve such conflict?