Permalink
Browse files

Code cleanup.

  • Loading branch information...
1 parent 0ec33cd commit 3aa389f9b659d02a117f77fc7e7cbbc7e05b0883 @Haacked committed Jan 2, 2012
@@ -1,5 +1,4 @@
using System.Reflection;
-using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
@@ -53,7 +53,9 @@
<Compile Include="ValidateJsonAntiForgeryTokenAttribute.cs" />
</ItemGroup>
<ItemGroup>
- <None Include="MvcHaack.Ajax.nuspec" />
+ <None Include="MvcHaack.Ajax.nuspec">
+ <SubType>Designer</SubType>
+ </None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" />
@@ -5,34 +5,43 @@
using System.Web.Mvc;
using System.Web.Routing;
-namespace MvcHaack.ControllerInspector {
- internal class ControllerDetailer {
- public string GetControllerDetails(ControllerDescriptor controllerDescriptor, RequestContext requestContext) {
- var template = new ControllerDetails {
+namespace MvcHaack.ControllerInspector
+{
+ internal class ControllerDetailer
+ {
+ public string GetControllerDetails(ControllerDescriptor controllerDescriptor, RequestContext requestContext)
+ {
+ var template = new ControllerDetails
+ {
Model = GetControllerModel(controllerDescriptor, requestContext)
};
return template.TransformText();
}
- private static object GetControllerModel(ControllerDescriptor controllerDescriptor, RequestContext requestContext) {
- return new {
+ private static object GetControllerModel(ControllerDescriptor controllerDescriptor, RequestContext requestContext)
+ {
+ return new
+ {
ControllerName = controllerDescriptor.ControllerName,
- ControllerType = new {
+ ControllerType = new
+ {
Name = controllerDescriptor.ControllerType.Name,
Namespace = controllerDescriptor.ControllerType.Namespace,
Attributes = GetAttributesModel(controllerDescriptor.GetCustomAttributes(inherit: true))
},
Actions = from action in controllerDescriptor.GetCanonicalActions()
let reflectedAction = action as ReflectedActionDescriptor
- select new {
+ select new
+ {
Name = action.ActionName,
Id = GetActionId(action),
Verbs = GetVerbs(action),
Path = GetSamplePath(requestContext, action),
MethodInfo = (reflectedAction != null ? reflectedAction.MethodInfo : null),
ReturnType = (reflectedAction != null ? reflectedAction.MethodInfo.ReturnType : null),
Parameters = from parameter in action.GetParameters()
- select new {
+ select new
+ {
Name = parameter.ParameterName,
Type = parameter.ParameterType,
IsComplexType = IsComplexType(parameter.ParameterType),
@@ -44,62 +53,73 @@ internal class ControllerDetailer {
GlobalFilters = from filter in GlobalFilters.Filters
let filterInstance = filter.Instance
let filterType = filterInstance.GetType()
- select new {
+ select new
+ {
Name = filterType.Name,
Namespace = filterType.Namespace,
Properties = from property in filterType.GetProperties()
- select new {
+ select new
+ {
Name = property.Name,
Value = property.GetValue(filterInstance, null)
}
}
};
}
- private static IEnumerable<object> GetPropertiesModel(object o, Type t) {
+ private static IEnumerable<object> GetPropertiesModel(object o, Type t)
+ {
return from property in t.GetProperties()
- select new {
+ select new
+ {
Name = property.Name,
Value = property.GetValue(o, null)
};
}
- private static IEnumerable<object> GetAttributesModel(IEnumerable<object> attributes) {
+ private static IEnumerable<object> GetAttributesModel(IEnumerable<object> attributes)
+ {
return from attribute in attributes
let type = attribute.GetType()
- select new {
+ select new
+ {
Name = type.Name,
Type = type,
Namespace = type.Namespace,
Properties = GetPropertiesModel(attribute, type)
};
}
- private static IEnumerable<string> GetVerbs(ActionDescriptor action) {
+ private static IList<string> GetVerbs(ActionDescriptor action)
+ {
var reflectedActionDescriptor = action as ReflectedActionDescriptor;
- if (reflectedActionDescriptor != null) {
+ if (reflectedActionDescriptor != null)
+ {
var selectors = (ActionMethodSelectorAttribute[])reflectedActionDescriptor.MethodInfo.GetCustomAttributes(typeof(ActionMethodSelectorAttribute), inherit: true);
- return from selector in selectors
- where selector is HttpPostAttribute
- || selector is HttpGetAttribute
- || selector is HttpPutAttribute
- || selector is HttpDeleteAttribute
- select selector.GetType().Name.Replace("Http", "").Replace("Attribute", "");
+ return (from selector in selectors
+ where selector is HttpPostAttribute
+ || selector is HttpGetAttribute
+ || selector is HttpPutAttribute
+ || selector is HttpDeleteAttribute
+ select selector.GetType().Name.Replace("Http", "").Replace("Attribute", "")).ToList();
}
return new string[] { };
}
- internal static string GetActionId(ActionDescriptor actionDescriptor) {
+ internal static string GetActionId(ActionDescriptor actionDescriptor)
+ {
string actionName = actionDescriptor.ActionName;
var verbs = GetVerbs(actionDescriptor);
- if (verbs.Any()) {
+ if (verbs.Any())
+ {
actionName = actionName + ":" + String.Join(":", verbs);
}
return actionName;
}
- private static string GetSamplePath(RequestContext requestContext, ActionDescriptor action) {
+ private static string GetSamplePath(RequestContext requestContext, ActionDescriptor action)
+ {
var urlHelper = new UrlHelper(requestContext);
var actionNameAttrib = action.GetCustomAttributes(inherit: true).OfType<ActionNameAttribute>().FirstOrDefault();
@@ -108,7 +128,8 @@ internal class ControllerDetailer {
// e.g. they could come from a POST body.
// In that case, they may end up as bogus query string params on the path, which is a bit buggy
var routeValues = new RouteValueDictionary();
- foreach (ParameterDescriptor param in action.GetParameters()) {
+ foreach (ParameterDescriptor param in action.GetParameters())
+ {
routeValues.Add(param.ParameterName, GetDefaultValue(param));
}
@@ -118,14 +139,17 @@ internal class ControllerDetailer {
routeValues);
}
- public static object GetDefaultValue(ParameterDescriptor param) {
+ public static object GetDefaultValue(ParameterDescriptor param)
+ {
// If it's a string, giving some value based on the param name
- if (param.ParameterType == typeof(string)) {
+ if (param.ParameterType == typeof(string))
+ {
return String.Format("Some{0}", param.ParameterName);
}
// If it's a number, pick some sample number.
- switch (Type.GetTypeCode(param.ParameterType)) {
+ switch (Type.GetTypeCode(param.ParameterType))
+ {
case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.UInt16:
@@ -141,29 +165,33 @@ internal class ControllerDetailer {
}
// For other value types, go with the default value
- if (param.ParameterType.IsValueType) {
+ if (param.ParameterType.IsValueType)
+ {
return Activator.CreateInstance(param.ParameterType);
}
return null;
}
- private static IEnumerable<object> GetInputModels(ControllerDescriptor controller) {
+ private static IEnumerable<object> GetInputModels(ControllerDescriptor controller)
+ {
var models = from action in controller.GetCanonicalActions()
let parameters = action.GetParameters()
from parameter in parameters
let type = parameter.ParameterType
// using definition of complex type from ModelMetadata class
where IsComplexType(type)
- select new {
+ select new
+ {
Name = type.Name,
FullName = type.FullName,
Properties = type.GetProperties()
};
return models;
}
- private static bool IsComplexType(Type type) {
+ private static bool IsComplexType(Type type)
+ {
return type != typeof(object) && !TypeDescriptor.GetConverter(type).CanConvertFrom(typeof(string));
}
}
@@ -1,17 +1,22 @@
using System.Web.Mvc;
-namespace MvcHaack.ControllerInspector {
- public class InspectorActionInvoker : ControllerActionInvoker {
- IActionInvoker _invoker;
+namespace MvcHaack.ControllerInspector
+{
+ public class InspectorActionInvoker : ControllerActionInvoker
+ {
+ readonly IActionInvoker _invoker;
- public InspectorActionInvoker(IActionInvoker invoker) {
+ public InspectorActionInvoker(IActionInvoker invoker)
+ {
_invoker = invoker;
}
- public override bool InvokeAction(ControllerContext controllerContext, string actionName) {
+ public override bool InvokeAction(ControllerContext controllerContext, string actionName)
+ {
var httpContext = controllerContext.HttpContext;
var controllerActionInvoker = _invoker as ControllerActionInvoker;
- if (controllerActionInvoker != null) {
+ if (controllerActionInvoker != null)
+ {
var detailer = new ControllerDetailer();
httpContext.Response.Write(detailer.GetControllerDetails(GetControllerDescriptor(controllerContext), controllerContext.RequestContext));
return true;
@@ -4,36 +4,47 @@
using System.Web.Routing;
using System.Web.SessionState;
-namespace MvcHaack.ControllerInspector {
- public class InspectorControllerFactory : IControllerFactory {
- IControllerFactory _controllerFactory;
- public InspectorControllerFactory(IControllerFactory controllerFactory) {
+namespace MvcHaack.ControllerInspector
+{
+ public class InspectorControllerFactory : IControllerFactory
+ {
+ readonly IControllerFactory _controllerFactory;
+ public InspectorControllerFactory(IControllerFactory controllerFactory)
+ {
_controllerFactory = controllerFactory;
}
- public IController CreateController(RequestContext requestContext, string controllerName) {
+ public IController CreateController(RequestContext requestContext, string controllerName)
+ {
var controller = _controllerFactory.CreateController(requestContext, controllerName);
- if (IsInspectorRequest(requestContext.HttpContext.Request)) {
+ if (IsInspectorRequest(requestContext.HttpContext.Request))
+ {
var normalController = controller as Controller;
- var invoker = normalController.ActionInvoker;
- normalController.ActionInvoker = new InspectorActionInvoker(invoker);
+ if (normalController != null)
+ {
+ var invoker = normalController.ActionInvoker;
+ normalController.ActionInvoker = new InspectorActionInvoker(invoker);
+ }
}
return controller;
}
- private static bool IsInspectorRequest(HttpRequestBase httpRequest) {
+ private static bool IsInspectorRequest(HttpRequestBase httpRequest)
+ {
return httpRequest.IsLocal
&& httpRequest.QueryString.Keys.Count > 0
&& httpRequest.QueryString.GetValues(null) != null
&& httpRequest.QueryString.GetValues(null).Contains("inspect");
}
- public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName) {
+ public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
+ {
return _controllerFactory.GetControllerSessionBehavior(requestContext, controllerName);
}
- public void ReleaseController(IController controller) {
+ public void ReleaseController(IController controller)
+ {
_controllerFactory.ReleaseController(controller);
}
}
@@ -1,5 +1,4 @@
using System.Reflection;
-using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following

0 comments on commit 3aa389f

Please sign in to comment.