I need to create a route where the user can open a specific page such as
http://www.mywebsite.com/profilename
When this is inputted the page which will show a profile with correct information should be shown. Basically like facebook.
What is the route which I need to create. I did this but then there will be a redirect loop when for example someone types http://www.mywebsite.com/users, which will for example show a list of users
routes.MapRoute(
name: "HomeIndexWithParam",
url: "{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
You will need to create several routes to get this working. When working with routes, remember that order matters, so place the most specific routes first.
First, you’ll create the routes for predetermined controllers. These are routes that are the same all the time. In this case, the “Users” route:
Then you’ll follow that with the route that will be responsible for the user profiles:
I would recommend that if the “username” follows a predetermined pattern (username is between 4 and 16 characters, only allow numbers and text, etc.), that you add a constrain so it only executes when the constraint is met
In the Profile action method in the user controller, you will take in the username and if the username is found, return the view for the user’s profile page. Otherwise redirect the user to the home page.
Finally, you’ll have the default route:
Hope this makes sense.