Skip to content

Instantly share code, notes, and snippets.

@stuartleeks
Created May 17, 2014 05:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuartleeks/324162793413f0e8d19e to your computer and use it in GitHub Desktop.
Save stuartleeks/324162793413f0e8d19e to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8" ?>
@using System.Web.Http
@using WebApiWadl.Areas.HelpPage.Models
@using WebApiWadl.Helpers;
@using System.Web.Http.Description;
@model IEnumerable<HelpPageApiModel>
@{
Layout = null;
Response.ContentType = "application/xml";
var hostUrl = Html.ViewContext.HttpContext.Request.Url;
}
<application xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" @*xsi:schemalocation="http://wadl.dev.java.net/2009/02 http://www.w3.org/Submission/wadl/wadl.xsd"*@ xmlns="http://wadl.dev.java.net/2009/02">
<doc title="api" />
<resources base="@hostUrl.Scheme://@hostUrl.Host">
@foreach (var api in Model)
{
<resource path="@api.ApiDescription.RelativePath">
<doc title="@api.ApiDescription.RelativePath" /> @* TODO - look at ways to allow specifying doc titles *@
<method name="@api.ApiDescription.HttpMethod">
<request>
@foreach (var param in api.UriParameters)
{
<param name="@param.Name"
style="template"
type="xs:@(param.TypeDescription.ModelType.ToXmlTypeString() ?? "string")"
required="@(param.Annotations.Any(p=>p.Documentation == "Required") ? "true" : "false")" /> @*TODO type mapping needs work!!*@
}
@foreach (var sampleRequest in api.SampleRequests)
{
<representation mediaType="@sampleRequest.Key.MediaType">
<doc>@sampleRequest.Value</doc>
</representation>
}
</request>
@if (api.SampleResponses.Any())
{
<response status="200">
@* TODO can we identify non-200 responses? *@
@foreach (var sampleResponse in api.SampleResponses)
{
<representation mediaType="@sampleResponse.Key.MediaType">
<doc>@sampleResponse.Value</doc>
</representation>
}
</response>
}
</method>
</resource>
}
</resources>
</application>
@uncarlo
Copy link

uncarlo commented Jun 25, 2014

My visual studio complains about not being able to resolve UriParameters (and of course all of its subsequent properties). Any solution for that? This is what my HelpPageApiModel class looks like (notice it's missing UriParameters):

///

/// The model that represents an API displayed on the help page.
///

public class HelpPageApiModel
{
///

/// Initializes a new instance of the <see cref="HelpPageApiModel"/> class.
/// </summary>


public HelpPageApiModel()
{
    SampleRequests = new Dictionary<MediaTypeHeaderValue, object>();
    SampleResponses = new Dictionary<MediaTypeHeaderValue, object>();
    ErrorMessages = new Collection<string>();
}
/// <summary>
/// Gets or sets the <see cref="ApiDescription"/> that describes the API.
/// </summary>
public ApiDescription ApiDescription { get; set; }

/// <summary>
/// Gets the sample requests associated with the API.
/// </summary>
public IDictionary<MediaTypeHeaderValue, object> SampleRequests { get; private set; }

/// <summary>
/// Gets the sample responses associated with the API.
/// </summary>
public IDictionary<MediaTypeHeaderValue, object> SampleResponses { get; private set; }

/// <summary>
/// Gets the error messages associated with this model.
/// </summary>
public Collection<string> ErrorMessages { get; private set; }

}

@rkierkels
Copy link

Same problem with Uriparameters.
This works:

  1. Comment out the foreach loop with Uriparameters.
  2. Replace foreach loop with this:
@foreach (var param in api.ApiDescription.ParameterDescriptions)
{
      <param name="@param.Name"
      style="template"
      type="xs:@(param.ParameterDescriptor.ParameterType.ToXmlTypeString() ?? "string")"
      required="@(param.ParameterDescriptor.IsOptional ? "false" : "true")" />
}

UPDATE:
If you create a MVC 5.1 WebApi project with .NET 4.5.1 framework it will work out of the box without the above fix. This fix is only required if you use .NET 4.5 with MVC 4.

@garkenxian
Copy link

I was having an issue with the azure api management utility working with this format. It was giving me WADL formatting errors. I had to modify the view to this below.

Thanks though, everything else worked like a charm.

<?xml version="1.0" encoding="utf-8" ?>
@using Telular.Data.CommonBusiness.WebAPI.Areas.HelpPage.Helpers
@model IEnumerable<Telular.Data.CommonBusiness.WebAPI.Areas.HelpPage.Models.HelpPageApiModel>
@{
    Layout = null;
    Response.ContentType = "application/xml";
    var hostUrl = Html.ViewContext.HttpContext.Request.Url;
}
<application xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" @*xsi:schemalocation="http://wadl.dev.java.net/2009/02 http://www.w3.org/Submission/wadl/wadl.xsd"*@ xmlns="http://wadl.dev.java.net/2009/02">
    <doc title="api" />
    <resources base="@hostUrl.Scheme://@hostUrl.Host">
        @foreach (var api in Model)
            {
                <resource path="@api.ApiDescription.RelativePath">
                    <doc title="@api.ApiDescription.RelativePath" /> @* TODO - look at ways to allow specifying doc titles *@
                    @foreach (var param in api.UriParameters)
                    {
                    <param name="@param.Name"
                            style="template"
                            type="xs:@(param.TypeDescription.ModelType.ToXmlTypeString() ?? "string")"
                            required="@(param.Annotations.Any(p => p.Documentation == "Required") ? "true" : "false")"/> @*TODO type mapping needs work!!*@
                    }

                    <method name="@api.ApiDescription.HttpMethod">
                        @if (api.SampleRequests.Any())
                        {
                            <request>
                                @foreach (var sampleRequest in api.SampleRequests)
                                {
                                    <representation mediaType="@sampleRequest.Key.MediaType">
                                        <doc>@sampleRequest.Value</doc>
                                    </representation>
                                }
                            </request>
                        }

                        @if (api.SampleResponses.Any())
                        {
                            <response status="200">
                                @* TODO can we identify non-200 responses? *@
                                @foreach (var sampleResponse in api.SampleResponses)
                                {
                                    <representation mediaType="@sampleResponse.Key.MediaType">
                                        <doc>@sampleResponse.Value</doc>
                                    </representation>
                                }
                            </response>
                        }
                    </method>
                </resource>
        }
    </resources>
</application>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment