如何保存具有唯一名称的页面?

时间:2012-02-28 12:46:44

标签: c# asp.net

我正在使用C#和sql数据库。在db中存储有关项目的信息 - id,name等

现在我有一个显示项目的页面,我用作模板并在URL中传递Item id,如/Item.aspx?id=223。 我想为每个项目创建页面并将它们保存到文件夹,如/Items/Red-Book.aspx。 有什么建议吗?

谢谢,Walt

1 个答案:

答案 0 :(得分:0)

您无需物理保存页面。您只需要一个页面Item.aspx,它将根据您提供的ItemiId生成内容。 (就像你在问题中解释的那样)

拥有此页面后,您需要了解如何重写URL。

重写URL所做的是它需要用户请求的url并将其重写为其他内容。

如果用户请求 /Items/Red-Book.aspx ,则网址将重新写入/Item.aspx?id=223,并且您将加载 /Item.aspx ?id = 223 回到客户端。

您可以使用可以在Application_BeginRequest中调用的RewritePath方法来重写您的URL。逻辑如下:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    var currentPath = HttpContext.Current.Request.Path;
    // do some logic to generate newPath
    var newPath = GetNewPath ( currentPath );
    HttpContext.Current.RewritePath(newpath);
    // after this point ASP.NET will work as the user would have requested newpath
}