gpt4 book ai didi

asp.net - 如何在 URL 中添加用户名(即 URL 重写)?

转载 作者:行者123 更新时间:2023-12-02 14:02:42 25 4
gpt4 key购买 nike

成功注册/登录后,我希望在网址中包含用户的用户名,如下所示

http://myweb.com/username/255
http://myweb.com/username/settings

我正在使用 ASP.Net 重写模块,这是我当前使用的重写规则之一,它生成 http://myweb.com/home/255:

<rule name="HomeRewrite" stopProcessing="true">
<match url="^home$"/>
<conditions>
<add input="{URL}" pattern="\.axd$" negate="true"/>
</conditions>
<action type="Rewrite" url="/home.aspx"/>
</rule>

我试图改变自己:

<rule name="HomeRewrite" stopProcessing="true">
<match url="^{R:1}/home$"/>
<conditions>
<add input="{URL}" pattern="\.axd$" negate="true"/>
</conditions>
<action type="Rewrite" url="/home.aspx"/>
</rule>

但它不起作用,我知道我做错了。那么在这方面有什么帮助吗?

最佳答案

尽管ASP.NET Routing 是 MVC 的一部分,您也可以在 ASP.Net 表单应用程序中使用该机制(请参阅 https://web.archive.org/web/20201205221404/https://www.4guysfromrolla.com/articles/051309-1.aspx )

例如在 Global.asax 的 void Application_Start(object sender, EventArgs e)您可以添加此代码

RouteTable.Routes.Add("Users", new Route("{username}/settings", 
new CustomRouteHandler()));

其中自定义类 CustomRouteHandler 实现 IRouteHandler 将请求发送到您的设置代码( View )。

您只需要在用户通过身份验证后立即将其重定向到该网址(并将其保留在那里)。在 Global.asax 的 Application_BeginRequest(object sender, EventArgs e)

关于asp.net - 如何在 URL 中添加用户名(即 URL 重写)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9159734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com