Friday 13 January 2017

Creating Azure WebApi with Swagger.

First create  WebApplication add Azure Api App Template and Select MVC.
Click OK.

Open URL https://www.nuget.org/packages/Swashbuckle to get new Swashbuckle
To install Swashbuckle - Swagger for WebApi, run the following command in the Package Manager Console
PM > Install-Package Swashbuckle -Version 5.5.3
Open SwaggerConfig.cs from App_start-> SwaggerConfig.cs.
Un comment the bellow line
//c.IncludeXmlComments(GetXmlCommentsPath());
And Replace GetXmlCommentsPath() with the following
c.IncludeXmlComments(string.Format(@"0}\bin\AzureApiSwagger.XML",System.AppDomain.CurrentDomain.BaseDirectory));

Go to the application properties -> Build -> check “XML documentation file, copy the provided path from the text box and REPLACE. AND SAVE.

Go to global.asx page and comment the following

//RouteConfig.RegisterRoutes(RouteTable.Routes);
And save

Next Go to Web Api application
1.    Add Install-Package Microsoft.AspNet.WebApi.HelpPage
2.    for Help Page which is on

Next Add Install-Package WebApiTestClient   for Designing Test API

For Unit Resolver You add This thing
Install-Package Unity -Version 2.1.505.2

Open Api.cshtml from Areas->HelpPage->Views->Help-> Api.cshtml
Add the following line in the end of the file
@Html.DisplayForModel("TestClientDialogs"
@Html.DisplayForModel("TestClientReferences")

Next Run the web Api .
And next you see this screen.


And next copy that url and run on Internet Explorer.

And next save it.
After you go to download file you can see


That v1 screen you can get.
Next Process we use in Azure we can give next process


If you want to By deafault run our swasgger file without Writing any URl ten Write this code.
using Swashbuckle.Application;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Http;

namespace AzureApiSwagger
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            //routes.MapRoute(
            //    name: "Default",
            //    url: "{controller}/{action}/{id}",
            //    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            //);

            routes.MapHttpRoute(
            name: "swagger_root",
            routeTemplate: "",
            defaults: null,
            constraints: null,
            handler: new RedirectHandler((message => message.RequestUri.ToString()), "swagger"));
        }
    }
}

Next Process we use in Global.asax gile add below code
=====================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;

namespace nApi
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
    }

}


No comments:

Post a Comment