-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathMvcData.cs
More file actions
executable file
·146 lines (136 loc) · 5.29 KB
/
MvcData.cs
File metadata and controls
executable file
·146 lines (136 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System.Collections.Generic;
using System.Linq;
using Sdl.Web.Common.Configuration;
using System;
namespace Sdl.Web.Common.Models
{
/// <summary>
/// Represents data about the Model, View and Controller
/// </summary>
public class MvcData
{
public string ControllerName { get; set; }
public string ControllerAreaName { get; set; }
public string ActionName { get; set; }
public string ViewName { get; set; }
public string AreaName { get; set; }
public string RegionName { get; set; }
public string RegionAreaName { get; set; } // TODO: Do we really need this?
public Dictionary<string, string> RouteValues { get; set; }
#region Constructors
/// <summary>
/// Initializes a new empty <see cref="MvcData"/> instance.
/// </summary>
public MvcData()
{
}
/// <summary>
/// Initializes a new <see cref="MvcData"/> instance for a given qualified View name.
/// </summary>
/// <param name="qualifiedViewName">The qualified View name with format AreaName:ControllerName:ViewName.</param>
public MvcData(string qualifiedViewName)
{
string[] qualifiedViewNameParts = qualifiedViewName.Split(':');
switch (qualifiedViewNameParts.Length)
{
case 1:
AreaName = SiteConfiguration.GetDefaultModuleName();
ViewName = qualifiedViewNameParts[0];
break;
case 2:
AreaName = qualifiedViewNameParts[0];
ViewName = qualifiedViewNameParts[1];
break;
case 3:
AreaName = qualifiedViewNameParts[0];
ControllerName = qualifiedViewNameParts[1];
ViewName = qualifiedViewNameParts[2];
break;
default:
throw new DxaException(
string.Format("Invalid format for Qualified View Name: '{0}'. Format must be 'ViewName' or 'AreaName:ViewName' or 'AreaName:ControllerName:Vieweame.'",
qualifiedViewName)
);
}
}
/// <summary>
/// Initializes a new <see cref="MvcData"/> instance which is a copy of another.
/// </summary>
/// <param name="other">The other <see cref="MvcData"/> to copy.</param>
public MvcData(MvcData other)
{
ControllerName = other.ControllerName;
ControllerAreaName = other.ControllerAreaName;
ActionName = other.ActionName;
ViewName = other.ViewName;
AreaName = other.AreaName;
RegionName = other.RegionName;
RegionAreaName = other.RegionAreaName;
if (other.RouteValues != null)
{
RouteValues = new Dictionary<string, string>(other.RouteValues);
}
}
#endregion
#region Overrides
/// <summary>
/// Determines whether the specified object is equal to the current object.
/// </summary>
/// <param name="obj">The object to compare with the current object. </param>
public override bool Equals(object obj)
{
MvcData other = obj as MvcData;
if (other == null)
{
return false;
}
// NOTE: RegionName and RegionAreaName are not included in equality check (these merely carry some additional info).
if ((ControllerName != other.ControllerName) ||
(ControllerAreaName != other.ControllerAreaName) ||
(ActionName != other.ActionName) ||
(ViewName != other.ViewName) ||
(AreaName != other.AreaName))
{
return false;
}
if (RouteValues == null)
{
return other.RouteValues == null;
}
return RouteValues.SequenceEqual(other.RouteValues);
}
/// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
/// <returns>
/// A hash code for the current object.
/// </returns>
public override int GetHashCode()
{
return unchecked (
SafeHashCode(ControllerName) ^
SafeHashCode(ControllerAreaName) ^
SafeHashCode(ActionName) ^
SafeHashCode(ViewName) ^
SafeHashCode(AreaName)
);
}
/// <summary>
/// Returns a string that represents the current object.
/// </summary>
/// <returns>
/// A string that represents the current object.
/// </returns>
public override string ToString()
{
// TODO: what about the other properties?
var routeValues = RouteValues != null ? String.Join(",", RouteValues.Select(v => String.Format("{0}:{1}", v.Key, v.Value))) : "";
return string.Format("{0}:{1}:{2}:[{3}]", AreaName, ControllerName, ViewName, routeValues);
}
#endregion
private static int SafeHashCode(object obj)
{
return (obj == null) ? 0 : obj.GetHashCode();
}
}
}