简体   繁体   English

如何仅用用户名在asp.net中进行URL重写

[英]how to do a url rewrite in asp.net with just a username

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. 我有一个asp.net Web应用程序,现在用户可以通过将int www.webdomain.com/page.aspx?usename=myusername放入该配置文件中。 I would like to change it to www.webdomain.com/username. 我想将其更改为www.webdomain.com/username。 Thanks for any help. 谢谢你的帮助。

Use the MVC routing. 使用MVC路由。 This is a good article on how to use the mvc routing with webforms: 这是一篇关于如何在Webforms中使用MVC路由的好文章:

http://msdn.microsoft.com/en-us/magazine/dd347546.aspx http://msdn.microsoft.com/en-us/magazine/dd347546.aspx

and

http://msdn.microsoft.com/en-us/library/cc668177.aspx http://msdn.microsoft.com/en-us/library/cc668177.aspx

Sample IRouteConstraint: 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: 并且,要在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. 我建议您修改代码,以便进行检查以确保输入的值是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. 您可以使用ASP.NET MVC并使用IRouteConstraint创建路由以确保用户名存在。

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. 您还可以创建一个IHttpModule来捕获Application_BeginRequest和对www.webdomain.com/username的处理请求,并将它们重新写入或TransferRequest到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. 您也可以像使用IHttpModule一样直接在Global.asax中执行代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM