1

我们正在为我们的网站使用 MVCSiteMapProvider。

这是场景......想象一下员工的页面,/employee/123. 这将包含员工详细信息以及一组需要123在 url 中维护的链接。

一些链接需要指向旧版 url,所以类似/demographics/?employeeID=123, 就是一个例子。其中大约有 30 个链接,因此必须在站点地图中维护这些链接。

任何有关如何解决此问题的建议都会有所帮助。

4

1 回答 1

1

您可以创建一个 DynamicNodeProvider 来处理这种情况。您只需要在循环时添加“相关”节点,以便它们全部组合在一起。id 存储在 RouteValues 中并为所有用户缓存,因此站点地图不会在请求之间忘记它。

public class EmployeeDynamicNodeProvider
    : DynamicNodeProviderBase
{
    CRUDExample db = new CRUDExample();

    public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)
    {
        var result = new List<DynamicNode>();

        var employees = db.Employee;
        foreach (var employee in employees)
        {
            var employeeKey = "Product_" + employee.Id.ToString();

            // Create the "Details" node for the employee
            var employeeNode = new DynamicNode(employeeKey, "EmployeeParentNodeKey", employee.Name, employee.Name, "Employee", "Details");

            // Set the "id" route value so the match will work.
            employeeNode.RouteValues.Add("id", employee.Id);

            // Add the node to the result
            result.Add(employeeNode);


            // Create the "Edit" node for the employee
            var employeeEditNode = new DynamicNode("EmployeeEdit_" + employee.Id.ToString(), employeeKey, "Edit", "Edit", "Employee", "Edit");

            // Set the "id" route value of the edit node
            employeeEditNode.RouteValues.Add("id", employee.Id);

            // Add the node to the result
            result.Add(employeeEditNode);


            // Create the "Delete" node for the employee
            var employeeDeleteNode = new DynamicNode("EmployeeDelete_" + employee.Id.ToString(), employeeKey, "Delete", "Delete", "Employee", "Delete");

            // Set the "id" route value of the delete node
            employeeDeleteNode.RouteValues.Add("id", employee.Id);

            // Add the node to the result
            result.Add(employeeDeleteNode);


            // Create the "Demographics" node for the employee
            var employeeDemographicsNode = new DynamicNode("EmployeeDemographics_" + employee.Id.ToString(), employeeKey, "Demographics", "Demographics", "Employee", "Demographics");

            // Set the "id" route value of the delete node
            employeeDemographicsNode.RouteValues.Add("id", employee.Id);

            // Add the node to the result
            result.Add(employeeDemographicsNode);
        }

        return result;
    }
}

至于 URL,是 System.Web.Routing 将路由值转换为 URL。但是,如果您的 URL 很复杂,您还有其他几个选择:

  1. 使用站点地图节点的 Url 属性而不是路由值。
  2. 创建自定义 URL 解析器来处理 URL 的特殊情况。
于 2013-08-20T22:36:26.947 回答