asp.net应用程序中的Twitter样式url



我正在开发一个类似twitter的应用程序。那么我该如何完成url呢,比如

twitter.com/username

这将打开与用户名相关的配置文件

我正在使用asp.net创建。

谢谢

如果你打算使用MVC Razor,它就像传递一个查询字符串参数到你的页面一样简单。例如:

public ActionResult Index(string name)
{
     //Get user information and pass to view
     User userDetails = SomeLogic.GetUser(name);
     return View(userDetails);
}

或者,如果使用ASP。NET Web Forms你必须通过添加Nuget包来使用友好的URL。教程可以在这里找到:http://blogs.technet.com/b/southasiamvp/archive/2014/03/31/guest-post-exploring-asp-net-friendlyurls.aspx

总的来说,您所需要做的就是向页面传递一个查询字符串参数。使用友好的URL将帮助您完成所需的URL格式。

<标题> 更新

从你的评论,我看到你正在使用Web表单,所以我将修改获得查询字符串值的答案。下面的代码不会以友好的格式呈现URL,但我希望您能够根据我上面提供的链接修改示例。

protected void Page_Load(object sender, EventArgs e)
{
   if (Request.QueryString["name"] != null && !String.IsNullOrEmpty(Request.QueryString["name"].ToString())
   {
       string myNameValue = Request.QueryString["name"].ToString();
       //Since you have the name in the querystring, pass value to method that retrieves the record
       User userDetails = SomeLogic.GetUser(name);
   }
}

所以URL将是:profile.com/profile.aspx?name=John.

但是正如我所说的,您需要修改代码来更改示例以使用友好的URL,可以在本教程中找到:http://blogs.technet.com/b/southasiamvp/archive/2014/03/31/guest-post-exploring-asp-net-friendlyurls.aspx

最新更新