Difference between == and === in JavaScript

The 3 equal signs mean “equality without type coercion”. Using the triple equals, the values must be equal in type as well.

0 == false   // true
0 === false  // false, because they are of a different type
1 == "1"     // true, automatic type conversion for value only
1 === "1"    // false, because they are of a different type
null == undefined // true
null === undefined // false
'0' == false // true
'0' === false // false

Creating API Help Pages

Install ASP.NET and Web Tools 2012.2 Update. This update integrates help pages into the Web API project template.

Next, create a new ASP.NET MVC 4 project and select the Web API project template. The project template creates an example API controller named ValuesController. The template also creates the API help pages. All of the code files for the help page are placed in the Areas folder of the project.

When you run the application, the home page contains a link to the API help page. From the home page, the relative path is /Help.

This link brings you to an API summary page.

The MVC view for this page is defined in Areas/HelpPage/Views/Help/Index.cshtml. You can edit this page to modify the layout, introduction, title, styles, and so forth.

The main part of the page is a table of APIs, grouped by controller. The table entries are generated dynamically, using the IApiExplorer interface. (I’ll talk more about this interface later.) If you add a new API controller, the table is automatically updated at run time.

The “API” column lists the HTTP method and relative URI. The “Description” column contains documentation for each API. Initially, the documentation is just placeholder text. In the next section, I’ll show you how to add documentation from XML comments.

Each API has a link to a page with more detailed information, including example request and response bodies.

Adding Help Pages to an Existing Project

You can add help pages to an existing Web API project by using NuGet Package Manager. This option is useful you start from a different project template than the “Web API” template.

From the Tools menu, select Library Package Manager, and then select Package Manager Console. In the Package Manager Console window, type one of the following commands:

For a C# application: Install-Package Microsoft.AspNet.WebApi.HelpPage

For a Visual Basic application: Install-Package Microsoft.AspNet.WebApi.HelpPage.VB

There are two packages, one for C# and one for Visual Basic. Make sure to use the one that matches your project.

This command installs the necessary assemblies and adds the MVC views for the help pages (located in the Areas/HelpPage folder). You’ll need to manually add a link to the Help page. The URI is /Help. To create a link in a razor view, add the following:

@Html.ActionLink("API", "Index", "Help", new { area = "" }, null)

Also, make sure to register areas. In the Global.asax file, add the following code to the Application_Start method, if it is not there already:

protected void Application_Start()
{
 // Add this code, if not present.
 AreaRegistration.RegisterAllAreas();

// ...
}

Adding API Documentation

By default, the help pages have placeholder strings for documentation. You can use XML documentation commentsto create the documentation. To enable this feature, open the file Areas/HelpPage/App_Start/HelpPageConfig.cs and uncomment the following line:

config.SetDocumentationProvider(new XmlDocumentationProvider(
 HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));

Now enable XML documentation. In Solution Explorer, right-click the project and select Properties. Select the Buildpage.

Under Output, check XML documentation file. In the edit box, type “App_Data/XmlDocument.xml”.

Next, open the code for the ValuesController API controller, which is defined in /Controllers/ValuesControler.cs. Add some documentation comments to the controller methods. For example:

/// <summary>
/// Gets some very important data from the server.
/// </summary>
public IEnumerable<string> Get()
{
 return new string[] { "value1", "value2" };
}

/// <summary>
/// Looks up some data by ID.
/// </summary>
/// <param name="id">The ID of the data.</param>
public string Get(int id)
{
 return "value";
}

Note

Tip: If you position the caret on the line above the method and type three forward slashes, Visual Studio automatically inserts the XML elements. Then you can fill in the blanks.

Now build and run the application again, and navigate to the help pages. The documentation strings should appear in the API table.

The help page reads the strings from the XML file at run time. (When you deploy the application, make sure to deploy the XML file.)

Under the Hood

The help pages are built on top of the ApiExplorer class, which is part of the Web API framework. The ApiExplorerclass provides the raw material for creating a help page. For each API, ApiExplorer contains an ApiDescription that describes the API. For this purpose, an “API” is defined as the combination of HTTP method and relative URI. For example, here are some distinct APIs:

  • GET /api/Products
  • GET /api/Products/{id}
  • POST /api/Products

If a controller action supports multiple HTTP methods, the ApiExplorer treats each method as a distinct API.

To hide an API from the ApiExplorer, add the ApiExplorerSettings attribute to the action and set IgnoreApi to true.

[ApiExplorerSettings(IgnoreApi=true)] public HttpResponseMessage Get(int id) { }

You can also add this attribute to the controller, to exclude the entire controller.

The ApiExplorer class gets documentation strings from the IDocumentationProvider interface. As you saw earlier, the Help Pages library provides an IDocumentationProvider that gets documentation from XML documentation strings. The code is located in /Areas/HelpPage/XmlDocumentationProvider.cs. You can get documentation from another source by writing your own IDocumentationProvider. To wire it up, call the SetDocumentationProviderextension method, defined in HelpPageConfigurationExtensions

ApiExplorer automatically calls into the IDocumentationProvider interface to get documentation strings for each API. It stores them in the Documentation property of the ApiDescription and ApiParameterDescription objects.

Orginal article https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/creating-api-help-pages

DateTimeOffset for SOAP

If you are serious about using SOAP, it won’t be long before you find out that if you need a full ISO date in the XML, it will not work using C# and SOAP, as DateTimeOffset is not supported from Microsoft.

Here is how you go about ensuring that you deliver an ISO DateTime formatted field in SOAP.

First, you’ll need to create a struct to hold the ISO 8601 Date Time Offset, and we will use IXmlSerializable interface which provides custom formatting for XML serialisation and deserialization.

public struct Iso8601SerializableDateTimeOffset : IXmlSerializable
 {
 public DateTimeOffset value;

public Iso8601SerializableDateTimeOffset(DateTimeOffset value)
 {
 this.value = value;
 }

public static implicit operator Iso8601SerializableDateTimeOffset(DateTimeOffset value)
 {
 return new Iso8601SerializableDateTimeOffset(value);
 }

public static implicit operator DateTimeOffset(Iso8601SerializableDateTimeOffset instance)
 {
 return instance.value;
 }

public static bool operator ==(Iso8601SerializableDateTimeOffset a, Iso8601SerializableDateTimeOffset b)
 {
 return a.value == b.value;
 }

public static bool operator !=(Iso8601SerializableDateTimeOffset a, Iso8601SerializableDateTimeOffset b)
 {
 return a.value != b.value;
 }

public static bool operator <(Iso8601SerializableDateTimeOffset a, Iso8601SerializableDateTimeOffset b)
 {
 return a.value < b.value;
 }

public static bool operator >(Iso8601SerializableDateTimeOffset a, Iso8601SerializableDateTimeOffset b)
 {
 return a.value > b.value;
 }

public override bool Equals(object o)
 {
 if (o is Iso8601SerializableDateTimeOffset)
 return value.Equals(((Iso8601SerializableDateTimeOffset)o).value);
 else if (o is DateTimeOffset)
 return value.Equals((DateTimeOffset)o);
 else
 return false;
 }

public override int GetHashCode()
 {
 return value.GetHashCode();
 }

public XmlSchema GetSchema()
 {
 return null;
 }

public void ReadXml(XmlReader reader)
 {
 var text = reader.ReadElementString();
 value = DateTimeOffset.ParseExact(text, format: "o", formatProvider: null);
 }

public override string ToString()
 {
 return value.ToString(format: "o");
 }

public string ToString(string format)
 {
 return value.ToString(format);
 }

public void WriteXml(XmlWriter writer)
 {
 writer.WriteString(value.ToString(format: "o"));
 }
 }

We also need to cater for Json Date Time Offsets too, so we will write a converter

public class UtcDateTimeOffsetConverter : Newtonsoft.Json.Converters.IsoDateTimeConverter
 {
 public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
 {
 if (value is Iso8601SerializableDateTimeOffset)
 {
 var date = (Iso8601SerializableDateTimeOffset)value;
 value = date.value;
 }
 base.WriteJson(writer, value, serializer);
 }

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
 {
 object value = base.ReadJson(reader, objectType, existingValue, serializer);
 if (value is Iso8601SerializableDateTimeOffset)
 {
 var date = (Iso8601SerializableDateTimeOffset)value;
 value = date.value;
 }
 return value;
 }
 }

Next, we need to implement it in an object model, with a slight twist as you can not use the Iso8601SerializableDateTimeOffset struct directly, to do this we need to wrap the result into a string property

public class Foo
 {
 public Guid Id { get; set; }

[JsonConverter(typeof(UtcDateTimeOffsetConverter))]
 [XmlElement("AcquireDate")]
 public string acquireDateForXml
 {
 get { return AcquireDate.ToString(); }
 set { AcquireDate = DateTimeOffset.Parse(value); }
 }

[XmlIgnore]
 public Iso8601SerializableDateTimeOffset? AcquireDate;
 }

That is it, job done

https://github.com/BryanAvery/DateTimeOffsetSOAP

DateTimeOffsetforSOAP

Bing Maps in an MVC Application

Here is a simple example of how to use Bing Maps in an MVC application.

I am using Bing Maps V8 which is the current version of Microsoft.

Also, nothing that document.ready will fire long before the map script loads as it loads asynchronously.  Sometimes document.ready will fire before the page is loaded which means the map div might not even be available. To overcome this, we are using the callback parameter of the map script UR: for example:

http://www.bing.com/api/maps/mapcontrol?callback=LoadMap

BingMapsDemo

The Microsoft documentation can be found here:

https://www.bing.com/api/maps/sdkrelease/mapcontrol/isdk/Overview#JS

To setup a Key you can do this quite easily at the Bing Maps Portal website:

https://www.bingmapsportal.com