如何为Asp.Net MVC文档? [英] How to generate documentation for Asp.Net MVC?

查看:120
本文介绍了如何为Asp.Net MVC文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用.NET 4.0 / preVIEW包2,我们可以为WCF REST帮助页面。

With .net 4.0/Preview kit 2, we can generate help pages for WCF REST.

反正我们能为MVC做?

Is there anyway we can do the same for MVC ?

www.somewebsite.com/search/help

www.somewebsite.com/search/help

我可以创建帮助页面(视图)和揭露他们。

I can create help pages (views) and expose them.

我可以生成XSD架构,并吐出为XML。

I can generate XSD schema and spit out as xml.

任何其他指导/建议?

我要生成类似这样的东西。

I want to generate something similar to this.

http://somewebsite.com/Search/

方法PUT

响应格式的XML

响应架构<一个href=\"http://somewebsite.com/help/response/schema\">http://somewebsite.com/help/response/schema

响应示例<一个href=\"http://somewebsite.com/help/response/example\">http://somewebsite.com/help/response/example

更新:

我可以使用下面code创建文档。

I was able to create documentation by using below code.

Route : somewebsite.com/Media/
HelpRoute : somewebsite.com/Media/Help (Append help to the parent route)

Add routes accordingly. See below example.

routes.MapRoute("Search",
           "Search/Quick/",
           new { controller = "Search", action = "Search" },
           new { httpMethod = new HttpMethodConstraint("PUT") }
           );

        routes.MapRoute("SearchHelp",
          "Search/Quick/Help",
          new { controller = "Search", action = "Search" },
          new { helpConstraint = new HelpConstraint { Help = true, SampleType = typeof(Search) } }
          );

 public class HelpResult : ViewResult
{
    private string HelpPage { get; set; }
    private string folderName = "HelpFiles";
    private HttpServerUtilityBase server { get; set; }
    private Type sampleType { get; set; }
    private string schemaName { get; set; }

    const string htmlFormat =
          @"
        <html>
        <head>
            <title>Help</title>
        </head>
        <body>         

        <li>URL - {0}</li>
        <li>Verb - {1}</li>
        {2}

        </body>
        </html>  
        ";

    public override void ExecuteResult(ControllerContext context)
    {
        server = context.HttpContext.Server;
        StringBuilder parentUrl = new StringBuilder();
        var data = context.RouteData.Route.GetRouteData(context.HttpContext);

        //Getting requested route url.
        string url = ((Route)(data.Route)).Url;

        //Getting parent route from requested route url by removing "Help" from the route.
        string newUrl = url.Substring(0, url.Length - 4);
        parentUrl.Append("/" + newUrl);

        sampleType = data.GetSampleType();
        var validVerbs = GetValidVerbs(MakeAppRelative(parentUrl.ToString()));
        CreateSchema(sampleType, true);
        HelpPage = string.Format(htmlFormat, newUrl, validVerbs, CreateInputSampleText());
        context.HttpContext.Response.Output.Write(HelpPage);
    }

    private string CreateInputSampleText()
    {
        if (sampleType != null && !string.IsNullOrEmpty(sampleType.Name))
        {
            string sampleText =
                @"<li>Input Sample Xml - <a href='\HelpFiles\{0}.xml'>Click Here</a></li>           
        <li>Input Sample Json - <a href='\HelpFiles\{0}.txt'>Click Here</a></li>
        <li>Input Schema - <a href='\HelpFiles\{1}'>Click Here</a></li>";
            sampleText = string.Format(sampleText, sampleType.Name, schemaName);
            return sampleText;
        }

        return string.Empty;
    }

    private static string MakeAppRelative(string url)
    {
        if (!url.StartsWith("~"))
        {
            if (!url.StartsWith("/"))
                url = "~/" + url;
            else
                url = "~" + url;
        }
        return url;
    }

    private static string GetValidVerbs(string Url)
    {
        StringBuilder validVerbs = new StringBuilder();
        var httpMethodOptions = new[] { "GET", "POST", "PUT", "DELETE", "HEAD" };
        foreach (var httpMethodOption in httpMethodOptions)
        {
            var fakeContext = new FakeHttpContext(MakeAppRelative(Url), httpMethodOption);
            foreach (Route route in RouteTable.Routes)
            {
                var rd = route.GetRouteData(fakeContext);
                if (rd != null)
                {
                    bool errorControllerApplied = route.IsErrorController();
                    if (!errorControllerApplied)
                    {
                        validVerbs.Append(httpMethodOption);
                    }
                }
            }
        }

        return validVerbs.ToString();
    }

    private void CreateFile(Type type, Stream stream)
    {
        using (Stream inputStream = stream)
        {
            schemaName = sampleType + "Schema.xml";
            string folder = Path.GetFullPath(Path.Combine(server.MapPath("~"), folderName));
            string file = Path.Combine(folder, schemaName);
            if (!Directory.Exists(folder))
                Directory.CreateDirectory(folder);

            using (FileStream fileStream = new FileStream(file, FileMode.Create))
            {
                byte[] fileContent = new byte[inputStream.Length];
                inputStream.Read(fileContent, 0, fileContent.Length);
                fileStream.Write(fileContent, 0, fileContent.Length);
                fileStream.Flush();
            }
        }
    }

    private void CreateSchema(Type type, bool isXmlSerializerType)
    {
        System.Collections.IEnumerable schemas;
        if (isXmlSerializerType)
        {
            XmlReflectionImporter importer = new XmlReflectionImporter();
            XmlTypeMapping typeMapping = importer.ImportTypeMapping(type);
            XmlSchemas s = new XmlSchemas();
            XmlSchemaExporter exporter = new XmlSchemaExporter(s);
            exporter.ExportTypeMapping(typeMapping);
            schemas = s.GetSchemas(null);
        }
        else
        {
            XsdDataContractExporter exporter = new XsdDataContractExporter();
            exporter.Export(type);
            schemas = exporter.Schemas.Schemas();
        }
        using (MemoryStream stream = new MemoryStream())
        {
            XmlWriterSettings xws = new XmlWriterSettings() { Indent = true };
            using (XmlWriter w = XmlWriter.Create(stream, xws))
            {
                w.WriteStartElement("Schemas");
                foreach (XmlSchema schema in schemas)
                {
                    if (schema.TargetNamespace != "http://www.w3.org/2001/XMLSchema")
                    {
                        schema.Write(w);
                    }
                }
            }
            stream.Seek(0, SeekOrigin.Begin);
            CreateFile(type, stream);
        }
    }
 public static class RouteDataExtensions
{
    public static bool IsHelpConstraintApplied(this RouteData data)
    {
        if (data != null && data.Route != null)
        {
            var constraints = ((Route) (data.Route)).Constraints;
            var helpConstraint = (from c in constraints.Values
                                  where c.GetType().Equals(typeof (HelpConstraint))
                                  select c).FirstOrDefault();
            if (helpConstraint != null)
            {
                return true;
            }
        }

        return false;
    }

    public static Type GetSampleType(this RouteData data)
    {
        if (data != null && data.Route != null)
        {
            var constraints = ((Route)(data.Route)).Constraints;
            var helpConstraint = (from c in constraints.Values
                                  where c.GetType().Equals(typeof(HelpConstraint))
                                  select c).FirstOrDefault();
            if (helpConstraint != null)
            {
                return ((HelpConstraint) helpConstraint).SampleType;
            }
        }

        return null;
    }

    public static string GetMethodType(this RouteData data)
    {
        if (data != null && data.Route != null)
        {
            var constraints = ((Route) (data.Route)).Constraints;
            var httpMethodConstraint = (from c in constraints.Values
                                        where c.GetType().Equals(typeof (HttpMethodConstraint))
                                        select c).FirstOrDefault();
            if (httpMethodConstraint != null)
            {
                return ((HttpMethodConstraint) httpMethodConstraint).AllowedMethods.Single();
            }
        }

        return null;
    }

    public static bool IsErrorController(this Route data)
    {
        if (data != null)
        {
            var defaults = ((Route)(data)).Defaults;
            var controllerName = (from c in defaults.Values
                                        where c.ToString().Contains("Error")
                                        select c).FirstOrDefault();
            if (controllerName != null)
            {
                return true;
            }
        }

        return false;
    }

    public static RouteData GetRouteDataByUrl(this string url)
    {
        string httpMethod = "PUT";
        var fakeContext = new FakeHttpContext(MakeAppRelative(url), httpMethod);
        return RouteTable.Routes.GetRouteData(fakeContext);
    }

    private static string MakeAppRelative(string url)
    {
        if (!url.StartsWith("~"))
        {
            if (!url.StartsWith("/"))
                url = "~/" + url;
            else
                url = "~" + url;
        }
        return url;
    }
}

 public class HelpConstraint : IRouteConstraint
{
    public bool Help { get; set; }
    public Type SampleType { get; set; }

    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        if(Help)
        {
            return true;
        }

        return false;
    }
}

References
http://stephenwalther.com/blog/archive/2008/08/03/asp-net-mvc-tip-29-build-a-controller-to-debug-your-custom-routes.aspx
http://aspnet.codeplex.com/releases/view/24644

这code是不是免费的错误。如果您有任何请张贴的改进。需要您自担风险使用它。

This code is not bug free. Please post improvements if you have any. Use it at your own risk.

推荐答案

最有可能你现在解决您的问题。无论如何,我认为你需要<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.http.description.iapiexplorer%28v=vs.108%29.aspx\"相对=nofollow> IApiExplorer 。看一看<一个href=\"http://blogs.msdn.com/b/yaohuang1/archive/2012/05/13/asp-net-web-api-introducing-iapiexplorer-apiexplorer.aspx\"相对=nofollow>这个博客。

Most probably you have solved your issue by now. Anyway, I think you need IApiExplorer. Have a look at this blog.

这篇关于如何为Asp.Net MVC文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆