1

我正在为我的 WebApi 使用 ASP.NET Web API 帮助页面,但不幸的是,使用ParameterBindingAttribute的控制器方法没有在GetApiExplorer(). 下面的例子GetOutPut是列出而GetEntrance不是。

public HelpController() : this(GlobalConfiguration.Configuration) { }

        public HelpController(HttpConfiguration config)
        {
            Configuration = config;
        }

        public ActionResult Index()
        {
            ViewBag.DocumentationProvider = Configuration.Services.GetDocumentationProvider();
            return View(Configuration.Services.GetApiExplorer().ApiDescriptions);
        }

控制器的方法

/// <summary>
        /// Entrance
        /// </summary>
        /// <param name="foo"></param>
        /// <param name="initialDate"></param>
        /// <param name="finalDate"></param>
        /// <returns></returns>
        [Route("entrance/{foo}/{initialDate}/{finalDate}")]
        [HttpGet]
        [ResponseType(typeof(BooModel))]
        public IHttpActionResult GetEntrance(string foo,
            [DateTimeParameter(DateFormat = DateTimeBindingFormats.yyyyMMddHHmm)] DateTime? initialDate,
            [DateTimeParameter(DateFormat = DateTimeBindingFormats.yyyyMMddHHmm)] DateTime? finalDate)
        {
            try
            {                
                return Ok();
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }

        /// <summary>
        /// Out Put
        /// </summary>
        /// <param name="foo"></param>
        /// <param name="initialDate"></param>
        /// <param name="finalDate"></param>
        /// <returns></returns>
        [Route("output/{foo}/{initialDate}/{finalDate}")]
        [HttpGet]
        [ResponseType(typeof(FooModel))]
        public IHttpActionResult GetOutPut(string foo, DateTime? initialDate, DateTime? finalDate)
        {
            try
            {                
                return Ok();
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }

XML文档.xml

<member name="M:IntegrationServices.Controllers.FooController.GetEntrance(System.String,System.Nullable{System.DateTime},System.Nullable{System.DateTime})">
            <summary>
            Entrance
            </summary>
            <param name="foo"></param>
            <param name="initialDate"></param>
            <param name="finalDate"></param>
            <returns></returns>
        </member>
        <member name="M:IntegrationServices.Controllers.FooController.GetOutPut(System.String,System.Nullable{System.DateTime},System.Nullable{System.DateTime})">
            <summary>
            Out Put
            </summary>
            <param name="foo"></param>
            <param name="initialDate"></param>
            <param name="finalDate"></param>
            <returns></returns>
        </member>

ParameterBindingAttribute 的类

 public class DateTimeParameterAttribute : ParameterBindingAttribute
    {
        public string DateFormat { get; set; }

        public bool ReadFromQueryString { get; set; }


        public override HttpParameterBinding GetBinding(HttpParameterDescriptor parameter)
        {
            if (parameter.ParameterType != typeof(DateTime?)) return parameter.BindAsError("Expected type DateTime?");
            var binding = new DateTimeParameterBinding(parameter)
            {
                DateFormat = DateFormat,
                ReadFromQueryString = ReadFromQueryString
            };

            return binding;
        }
    }

有任何想法吗 ?解决方法?

4

0 回答 0