I have an asp.net web application and right now users can get there profiles by putting int www.webdomain.com/page.aspx?usename=myusername. I would like to change it to www.webdomain.com/username. Thanks for any help.
Use the MVC routing. This is a good article on how to use the mvc routing with webforms:
http://msdn.microsoft.com/en-us/magazine/dd347546.aspx
and
Sample IRouteConstraint:
public class IsUserActionConstraint : IRouteConstraint
{
//This is a static variable that handles the list of users
private static List<string> _users;
//This constructor loads the list of users on the first call
public IsUserActionConstraint()
{
_users= (from u in Models.Users.Get() select u.Username.ToLower()).ToList();
}
//Code for checking to see if the route is a username
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
return _users.Contains((values["username"] as string).ToLower());
}
}
And, to register the route in the Global.asax:
routes.MapRoute(
"User Profile", // Route name
"{username}",
new { controller = "User", action = "Index", id = UrlParameter.Optional },// This stuff is here for ASP.NET MVC
new { IsUserAction = new IsUserActionConstraint() } //Your IRouteconstraint
);
In my case, my user list never changes during the application life cycle so I can cache it by using a static list. I would suggest that you modify the code so that you're doing what ever check to make sure the value entered is a username inside of the Match.
rewriterule www.webdomain.com/.+? www.webdomain.com/page.aspx?usename=$1
There are a few ways to do it. You could use ASP.NET MVC and create a route using a IRouteConstraint to make sure that the username exists.
You could also create an IHttpModule that captures the Application_BeginRequest and handel request for www.webdomain.com/username and ReWriting or TransferRequest them to www.webdomain.com/page.aspx?usename=myusername.
You could also do the code directly in the Global.asax the same way you would the IHttpModule.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.