Skip to content
desunit edited this page Jan 2, 2013 · 6 revisions

Swagger is a specification and complete framework implementation for describing, producing, consuming, and visualizing RESTful web services. ServiceStack implements basic API in a separate plugin which is available under NuGet package: ServiceStack.Api.Swagger or StackExpress.Api.Swagger (unstable build).

Installation

Default configuration expects that ServiceStack services are available under '/api' path. If it's a brand new MVC project install NuGet Package: (http://nuget.org/packages/ServiceStack.Host.Mvc/) which prepares ServiceStack default services. Make sure that you added ignore for MVC routes:

	routes.IgnoreRoute("api/{*pathInfo}"); 

If it's MVC4 project, then don't forget to disable WebAPI:

//WebApiConfig.Register(GlobalConfiguration.Configuration);

Enable Swagger plugin in AppHost.cs with:

    public override void Configure(Container container)
    {
        ...
        Plugins.Add(new SwaggerFeature());
	
        // uncomment CORS feature if it's has to be available from external sites 
        //Plugins.Add(new CorsFeature()); 
        ...
    }

Compile it. Now you can access swagger UI with:

http://localost:port/swagger-ui/index.html

or

http://yoursite/swagger-ui/index.html

Swagger Attributes

Each route could have a separate summary and description. You can set it with Route attribute:

[Route("/hello", Summary = @"Default hello service.", Notes = "Longer description for hello service.")]

You can set specific description for each HTTP method like shown below:

    [Route("/hello/{Name}", "GET", Summary = @"Says ""Hello"" to provided Name with GET.", 
        Notes = "Longer description of the GET method which says \"Hello\"")]
    [Route("/hello/{Name}", "POST", Summary = @"Says ""Hello"" to provided Name with POST.", 
        Notes = "Longer description of the POST method which says \"Hello\"")]

Request DTO properties could be decorated with ApiMethod attribute to specify what this attribute means.

        [ApiMember(Name = "Name", Description = "Name Description", ParameterType = "path", 
            DataType = "string", IsRequired = true)]
		public string Name { get; set; }

The complete list of available types could be find in Swagger documentation.

Clone this wiki locally