Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在asp.net中仅使用用户名重写url_C#_Asp.net_Url Rewriting - Fatal编程技术网

C# 如何在asp.net中仅使用用户名重写url

C# 如何在asp.net中仅使用用户名重写url,c#,asp.net,url-rewriting,C#,Asp.net,Url Rewriting,我有一个asp.net web应用程序,现在用户可以通过输入www.webdomain.com/page.aspx?usename=myusername来获取这些配置文件。我想将其更改为www.webdomain.com/username。感谢您的帮助。使用MVC路由。这是一篇关于如何在webforms中使用mvc路由的好文章: rewriterule www.webdomain.com/.+? www.webdomain.com/page.aspx?usename=$1 及 使用MVC路由

我有一个asp.net web应用程序,现在用户可以通过输入www.webdomain.com/page.aspx?usename=myusername来获取这些配置文件。我想将其更改为www.webdomain.com/username。感谢您的帮助。

使用MVC路由。这是一篇关于如何在webforms中使用mvc路由的好文章:

rewriterule www.webdomain.com/.+? www.webdomain.com/page.aspx?usename=$1


使用MVC路由。这是一篇关于如何在webforms中使用mvc路由的好文章:


有几种方法可以做到这一点。您可以使用ASP.NET MVC并使用IRouteConstraint创建路由,以确保用户名存在

您还可以创建一个IHTTP模块,用于捕获www.webdomain.com/username的应用程序_BeginRequest和handel请求,并将其重写或传输到www.webdomain.com/page.aspx?usename=myusername


您也可以直接在Global.asax中编写代码,方法与在IHTTP模块中编写代码的方法相同。

有几种方法。您可以使用ASP.NET MVC并使用IRouteConstraint创建路由,以确保用户名存在

您还可以创建一个IHTTP模块,用于捕获www.webdomain.com/username的应用程序_BeginRequest和handel请求,并将其重写或传输到www.webdomain.com/page.aspx?usename=myusername


您也可以直接在Global.asax中执行代码,方法与IHTTP模块相同。

示例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());
    }


}

在我的例子中,我的用户列表在应用程序生命周期中从不更改,因此我可以使用静态列表缓存它。我建议您修改代码,以便进行任何检查,以确保输入的值是匹配项中的用户名。

示例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());
    }


}


在我的例子中,我的用户列表在应用程序生命周期中从不更改,因此我可以使用静态列表缓存它。我建议您修改代码,以便进行任何检查,以确保输入的值是匹配项中的用户名。

我认为您要查找的单词是“重定向”。我认为您要查找的单词是“重定向”。很好的示例。我现在可以访问www.example.com/u/username。如何去掉/u而只使用/username,它说重新路由不能以/u开始,您需要为“{username}”注册一个路由,我建议创建一个iroute约束,当输入的值是用户名时,该约束将强制路由只匹配。你可以给我看一个asp.net格式的例子。我有IRouteConstraint类,但我不知道如何使用它。谢谢好例子。我现在可以访问www.example.com/u/username。如何去掉/u而只使用/username,它说重新路由不能以/u开始,您需要为“{username}”注册一个路由,我建议创建一个iroute约束,当输入的值是用户名时,该约束将强制路由只匹配。你可以给我看一个asp.net格式的例子。我有IRouteConstraint类,但我不知道如何使用它。谢谢,我还没有测试出来,因为我在将它转换为asp.net new{IsUserAction=new IsUserActionConstraint()}时遇到了问题。请发布您用于注册路由的完整代码块,我会给您一些帮助。new{}只是一个匿名对象或类型,在.NET3.0中引入了此功能。左侧只是一个变量名,右侧是创建类的实例。根据类文件的放置位置,您可能必须调整类的命名空间,例如使用asp.net 4.0调整new Path.to.class.IsUserActionConstraint()。并且该参数应为bool checkphysicalUrlAddress:routeCollection.MapPageRoute(“RouteForUser”、“{username}”、“~/Userprofile.aspx”、new IsUserActionConstraint());如果你从未调用match方法,它是如何运行的?好吧,我知道你不能在asp.net中运行它,只有在mvcI中才能测试,因为我在将它转换为asp.net new{IsUserAction=new IsUserActionConstraint()}时遇到了问题。上传你用来注册路由的完整代码块,我会给你一些帮助。new{}只是一个匿名对象或类型,在.NET3.0中引入了此功能。左侧只是一个变量名,右侧是创建类的实例。根据类文件的放置位置,您可能必须调整类的命名空间,例如使用asp.net 4.0调整new Path.to.class.IsUserActionConstraint()。并且该参数应为bool checkphysicalUrlAddress:routeCollection.MapPageRoute(“RouteForUser”、“{username}”、“~/Userprofile.aspx”、new IsUserActionConstraint());如果你从未调用match方法,它是如何运行的?好的,我知道你不能在asp.net中运行它,只能在mvc中运行