Skip to content

Commit cae7951

Browse files
Raj KamalRaj Kamal
Raj Kamal
authored and
Raj Kamal
committed
WebApi: Sample project added
1 parent fe175bd commit cae7951

File tree

88 files changed

+27675
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+27675
-0
lines changed

RestfulApi/App_Start/BundleConfig.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Web;
2+
using System.Web.Optimization;
3+
4+
namespace RestfulApi
5+
{
6+
public class BundleConfig
7+
{
8+
// For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
9+
public static void RegisterBundles(BundleCollection bundles)
10+
{
11+
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
12+
"~/Scripts/jquery-{version}.js"));
13+
14+
// Use the development version of Modernizr to develop with and learn from. Then, when you're
15+
// ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
16+
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
17+
"~/Scripts/modernizr-*"));
18+
19+
bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
20+
"~/Scripts/bootstrap.js",
21+
"~/Scripts/respond.js"));
22+
23+
bundles.Add(new StyleBundle("~/Content/css").Include(
24+
"~/Content/bootstrap.css",
25+
"~/Content/site.css"));
26+
}
27+
}
28+
}

RestfulApi/App_Start/FilterConfig.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Web;
2+
using System.Web.Mvc;
3+
4+
namespace RestfulApi
5+
{
6+
public class FilterConfig
7+
{
8+
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
9+
{
10+
filters.Add(new HandleErrorAttribute());
11+
}
12+
}
13+
}

RestfulApi/App_Start/RouteConfig.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Web;
5+
using System.Web.Mvc;
6+
using System.Web.Routing;
7+
8+
namespace RestfulApi
9+
{
10+
public class RouteConfig
11+
{
12+
public static void RegisterRoutes(RouteCollection routes)
13+
{
14+
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
15+
16+
routes.MapRoute(
17+
name: "Default",
18+
url: "{controller}/{action}/{id}",
19+
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
20+
);
21+
}
22+
}
23+
}

RestfulApi/App_Start/WebApiConfig.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Web.Http;
5+
6+
namespace RestfulApi
7+
{
8+
public static class WebApiConfig
9+
{
10+
public static void Register(HttpConfiguration config)
11+
{
12+
// Web API configuration and services
13+
14+
// Web API routes
15+
config.MapHttpAttributeRoutes();
16+
17+
config.Routes.MapHttpRoute(
18+
name: "DefaultApi",
19+
routeTemplate: "api/{controller}/{id}",
20+
defaults: new { id = RouteParameter.Optional }
21+
);
22+
}
23+
}
24+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Text;
3+
using System.Web;
4+
using System.Web.Http.Description;
5+
6+
namespace RestfulApi.Areas.HelpPage
7+
{
8+
public static class ApiDescriptionExtensions
9+
{
10+
/// <summary>
11+
/// Generates an URI-friendly ID for the <see cref="ApiDescription"/>. E.g. "Get-Values-id_name" instead of "GetValues/{id}?name={name}"
12+
/// </summary>
13+
/// <param name="description">The <see cref="ApiDescription"/>.</param>
14+
/// <returns>The ID as a string.</returns>
15+
public static string GetFriendlyId(this ApiDescription description)
16+
{
17+
string path = description.RelativePath;
18+
string[] urlParts = path.Split('?');
19+
string localPath = urlParts[0];
20+
string queryKeyString = null;
21+
if (urlParts.Length > 1)
22+
{
23+
string query = urlParts[1];
24+
string[] queryKeys = HttpUtility.ParseQueryString(query).AllKeys;
25+
queryKeyString = String.Join("_", queryKeys);
26+
}
27+
28+
StringBuilder friendlyPath = new StringBuilder();
29+
friendlyPath.AppendFormat("{0}-{1}",
30+
description.HttpMethod.Method,
31+
localPath.Replace("/", "-").Replace("{", String.Empty).Replace("}", String.Empty));
32+
if (queryKeyString != null)
33+
{
34+
friendlyPath.AppendFormat("_{0}", queryKeyString.Replace('.', '-'));
35+
}
36+
return friendlyPath.ToString();
37+
}
38+
}
39+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Uncomment the following to provide samples for PageResult<T>. Must also add the Microsoft.AspNet.WebApi.OData
2+
// package to your project.
3+
////#define Handle_PageResultOfT
4+
5+
using System;
6+
using System.Collections;
7+
using System.Collections.Generic;
8+
using System.Diagnostics;
9+
using System.Diagnostics.CodeAnalysis;
10+
using System.Linq;
11+
using System.Net.Http.Headers;
12+
using System.Reflection;
13+
using System.Web;
14+
using System.Web.Http;
15+
#if Handle_PageResultOfT
16+
using System.Web.Http.OData;
17+
#endif
18+
19+
namespace RestfulApi.Areas.HelpPage
20+
{
21+
/// <summary>
22+
/// Use this class to customize the Help Page.
23+
/// For example you can set a custom <see cref="System.Web.Http.Description.IDocumentationProvider"/> to supply the documentation
24+
/// or you can provide the samples for the requests/responses.
25+
/// </summary>
26+
public static class HelpPageConfig
27+
{
28+
[SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters",
29+
MessageId = "RestfulApi.Areas.HelpPage.TextSample.#ctor(System.String)",
30+
Justification = "End users may choose to merge this string with existing localized resources.")]
31+
[SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly",
32+
MessageId = "bsonspec",
33+
Justification = "Part of a URI.")]
34+
public static void Register(HttpConfiguration config)
35+
{
36+
//// Uncomment the following to use the documentation from XML documentation file.
37+
//config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));
38+
39+
//// Uncomment the following to use "sample string" as the sample for all actions that have string as the body parameter or return type.
40+
//// Also, the string arrays will be used for IEnumerable<string>. The sample objects will be serialized into different media type
41+
//// formats by the available formatters.
42+
//config.SetSampleObjects(new Dictionary<Type, object>
43+
//{
44+
// {typeof(string), "sample string"},
45+
// {typeof(IEnumerable<string>), new string[]{"sample 1", "sample 2"}}
46+
//});
47+
48+
// Extend the following to provide factories for types not handled automatically (those lacking parameterless
49+
// constructors) or for which you prefer to use non-default property values. Line below provides a fallback
50+
// since automatic handling will fail and GeneratePageResult handles only a single type.
51+
#if Handle_PageResultOfT
52+
config.GetHelpPageSampleGenerator().SampleObjectFactories.Add(GeneratePageResult);
53+
#endif
54+
55+
// Extend the following to use a preset object directly as the sample for all actions that support a media
56+
// type, regardless of the body parameter or return type. The lines below avoid display of binary content.
57+
// The BsonMediaTypeFormatter (if available) is not used to serialize the TextSample object.
58+
config.SetSampleForMediaType(
59+
new TextSample("Binary JSON content. See http://bsonspec.org for details."),
60+
new MediaTypeHeaderValue("application/bson"));
61+
62+
//// Uncomment the following to use "[0]=foo&[1]=bar" directly as the sample for all actions that support form URL encoded format
63+
//// and have IEnumerable<string> as the body parameter or return type.
64+
//config.SetSampleForType("[0]=foo&[1]=bar", new MediaTypeHeaderValue("application/x-www-form-urlencoded"), typeof(IEnumerable<string>));
65+
66+
//// Uncomment the following to use "1234" directly as the request sample for media type "text/plain" on the controller named "Values"
67+
//// and action named "Put".
68+
//config.SetSampleRequest("1234", new MediaTypeHeaderValue("text/plain"), "Values", "Put");
69+
70+
//// Uncomment the following to use the image on "../images/aspNetHome.png" directly as the response sample for media type "image/png"
71+
//// on the controller named "Values" and action named "Get" with parameter "id".
72+
//config.SetSampleResponse(new ImageSample("../images/aspNetHome.png"), new MediaTypeHeaderValue("image/png"), "Values", "Get", "id");
73+
74+
//// Uncomment the following to correct the sample request when the action expects an HttpRequestMessage with ObjectContent<string>.
75+
//// The sample will be generated as if the controller named "Values" and action named "Get" were having string as the body parameter.
76+
//config.SetActualRequestType(typeof(string), "Values", "Get");
77+
78+
//// Uncomment the following to correct the sample response when the action returns an HttpResponseMessage with ObjectContent<string>.
79+
//// The sample will be generated as if the controller named "Values" and action named "Post" were returning a string.
80+
//config.SetActualResponseType(typeof(string), "Values", "Post");
81+
}
82+
83+
#if Handle_PageResultOfT
84+
private static object GeneratePageResult(HelpPageSampleGenerator sampleGenerator, Type type)
85+
{
86+
if (type.IsGenericType)
87+
{
88+
Type openGenericType = type.GetGenericTypeDefinition();
89+
if (openGenericType == typeof(PageResult<>))
90+
{
91+
// Get the T in PageResult<T>
92+
Type[] typeParameters = type.GetGenericArguments();
93+
Debug.Assert(typeParameters.Length == 1);
94+
95+
// Create an enumeration to pass as the first parameter to the PageResult<T> constuctor
96+
Type itemsType = typeof(List<>).MakeGenericType(typeParameters);
97+
object items = sampleGenerator.GetSampleObject(itemsType);
98+
99+
// Fill in the other information needed to invoke the PageResult<T> constuctor
100+
Type[] parameterTypes = new Type[] { itemsType, typeof(Uri), typeof(long?), };
101+
object[] parameters = new object[] { items, null, (long)ObjectGenerator.DefaultCollectionSize, };
102+
103+
// Call PageResult(IEnumerable<T> items, Uri nextPageLink, long? count) constructor
104+
ConstructorInfo constructor = type.GetConstructor(parameterTypes);
105+
return constructor.Invoke(parameters);
106+
}
107+
}
108+
109+
return null;
110+
}
111+
#endif
112+
}
113+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Web.Http;
3+
using System.Web.Mvc;
4+
using RestfulApi.Areas.HelpPage.ModelDescriptions;
5+
using RestfulApi.Areas.HelpPage.Models;
6+
7+
namespace RestfulApi.Areas.HelpPage.Controllers
8+
{
9+
/// <summary>
10+
/// The controller that will handle requests for the help page.
11+
/// </summary>
12+
public class HelpController : Controller
13+
{
14+
private const string ErrorViewName = "Error";
15+
16+
public HelpController()
17+
: this(GlobalConfiguration.Configuration)
18+
{
19+
}
20+
21+
public HelpController(HttpConfiguration config)
22+
{
23+
Configuration = config;
24+
}
25+
26+
public HttpConfiguration Configuration { get; private set; }
27+
28+
public ActionResult Index()
29+
{
30+
ViewBag.DocumentationProvider = Configuration.Services.GetDocumentationProvider();
31+
return View(Configuration.Services.GetApiExplorer().ApiDescriptions);
32+
}
33+
34+
public ActionResult Api(string apiId)
35+
{
36+
if (!String.IsNullOrEmpty(apiId))
37+
{
38+
HelpPageApiModel apiModel = Configuration.GetHelpPageApiModel(apiId);
39+
if (apiModel != null)
40+
{
41+
return View(apiModel);
42+
}
43+
}
44+
45+
return View(ErrorViewName);
46+
}
47+
48+
public ActionResult ResourceModel(string modelName)
49+
{
50+
if (!String.IsNullOrEmpty(modelName))
51+
{
52+
ModelDescriptionGenerator modelDescriptionGenerator = Configuration.GetModelDescriptionGenerator();
53+
ModelDescription modelDescription;
54+
if (modelDescriptionGenerator.GeneratedModels.TryGetValue(modelName, out modelDescription))
55+
{
56+
return View(modelDescription);
57+
}
58+
}
59+
60+
return View(ErrorViewName);
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)