Пример #1
0
        public static string Create(WebInterfaceConfig config)
        {
            var sb = new StringBuilder();
            sb.AppendFormat("<html>{0}<body>", JS);

            sb.Append("<div style='width:100%;'><div class='left' style='float:left; width:20%;'>");

            foreach (var type in Reflection.GetAllTypes().OrderBy(t => t.Name))
            {
                ServiceAttribute serviceAttr;
                if (!type.TryGetAttribute(out serviceAttr))
                    continue;

                var serviceName = ServiceAttribute.GetName(type, serviceAttr).ToLower();

                foreach (var method in type.GetMethods())
                {
                    RouteAttribute methodAttr;
                    if (!method.TryGetAttribute(out methodAttr))
                        continue;

                    var anchor = serviceName + method.Name;
                    var url = string.Format("/{0}/{1}", serviceName, (methodAttr.Path ?? method.Name).ToLower());

                    var returnType = method.ReturnType;
                    if (returnType.IsSubclassOf(typeof(Task)) && returnType != typeof(Task))
                        returnType = returnType.GetGenericArguments()[0];

                    if (config.ReturnTypeModifier != null)
                        returnType = config.ReturnTypeModifier(returnType);

                    sb.AppendFormat("<h3>{1} <a href='#{0}' id='{0}'>{2}</a> => {3}</h3>", anchor, methodAttr.Verb.ToString().ToUpper(), url, WebUtility.HtmlEncode(returnType.GetFriendlyName()));

                    if (config.PathModifier != null)
                        url = config.PathModifier(url);

                    sb.AppendFormat("<form method='{0}' action='{1}' onsubmit='return submitForm(this)'/><table>", methodAttr.Verb, url);

                    var methodParams = method.GetParameters();
                    var paramNames = new HashSet<string>();

                    Action<string, string, Type> addParam = (name, value, paramType) =>
                    {
                        if (!paramNames.Add(name))
                            return;

                        sb.AppendFormat("<tr><td>{0} ({1})</td> <td><input type='text' name='{0}' {2}/></td></tr>", name, paramType.GetFriendlyName(), value);
                    };

                    foreach (var param in methodParams)
                    {
                        if (IgnoredTypes.Contains(param.ParameterType))
                            continue;

                        WebInterfaceParam webParam;
                        if (config.Swap == null || !config.Swap.TryGetValue(param.Name, out webParam))
                            webParam = new WebInterfaceParam(param.ParameterType, param.Name);

                        var value = string.Empty;

                        object defaultValue;
                        if (param.TryGetDefaultValue(out defaultValue))
                        {
                            value = defaultValue.ToString();
                        }
                        else if (config.Defaults != null)
                        {
                            config.Defaults.TryGetValue(webParam.Name, out value);
                        }

                        value = string.Format("value='{0}'", value);

                        addParam(webParam.Name, value, webParam.Type);
                    }

                    if (config.ExtraParamCreator != null)
                    {
                        var extraParams = config.ExtraParamCreator(method);

                        if (extraParams != null)
                        {
                            foreach (var extraParam in extraParams)
                                addParam(extraParam.Name, "", extraParam.Type);
                        }
                    }

                    sb.Append("</table><input type='submit' value='Submit'/></form>");
                }

                sb.Append("<hr/>");
            }

            sb.Append("</div>");
            sb.Append("<div class='right' style='float:right; width:80%;'><br/><button type='button' onclick='clearResult()'>Clear</button>");
            sb.Append("<hr/><p id='result' style='white-space: pre;'/></div></div>");
            sb.Append("</body></html>");
            return sb.ToString();
        }
Пример #2
0
		public void CreateWebInterface(WebInterfaceConfig config = null, string location = "/")
		{
			var page = WebInterface.Create(config ?? WebInterfaceConfig.Default);
			AddStaticPage(location, page);
		}