c#IApiExplorer没有使用ParameterBindingAttribute显示来自控制器的方法

时间:2017-10-19 20:34:38

标签: c# asp.net-web-api asp.net-web-api-helppages parameterbinding asp.net-mvc-apiexplorer

我正在为我的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);
            }
        }

XMLDocument.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;
        }
    }

有什么想法吗? workaroud?

0 个答案:

没有答案