Saturday, December 12, 2015

Spec an extra chunk of a route when hitting the Web API!

Alright, open Visual Studio 2015 and make a generic application which mixes MVC and the Web API. Quickly, set up the CORS stuff as suggested here and then rewrite the default "home page" view to look like this:

@{
   string fullLocation = HttpContext.Current.Request.Url.ToString();
   ViewBag.LocaleParts = fullLocation.Split("/".ToCharArray());
}
<script src="/Scripts/jquery-1.10.2.js" type="text/javascript"></script>
<script type="text/javascript">
   $(function () {
      var url = "http://";
      url = url + "@ViewBag.LocaleParts[2]";
      url = url + "/Api/Whatever/Foo";
      $.ajax({
         type: "POST",
         url: url,
         dataType: 'json',
         data: {Bar:'Baz'},
         success: function () {
            console.log('yay :)');
         },
         error: function () {
            console.log('nay :(');
         }
      });
   });
</script>

 
 

Clearly, we are trying to hand an API endpoint at "Whatever" two bits of data in two different ways. We are trying to pass in "Foo" as a chunk of the URL and we are trying to pass "Baz" as a setting on a JSON object. This is all doable. The "Whatever" Web API controller looks like this:

using System.Web.Http;
using System.Web.Http.Cors;
using SomethingSimple.Models;
namespace SomethingSimple.Controllers
{
   [EnableCors(origins: "*", headers: "*", methods: "*")]
   public class WhateverController : ApiController
   {
      public void Post(string id, Qux qux)
      {
         var breakpoint = "OK";
      }
   }
}

 
 

If we set a breakpoint at the line of code in the controller which has "OK" in it (and which does nothing whatsoever in and of itself) and we debug the application with the debugger, we ought to be able to hit the breakpoint and inspect both the "id" and "qux" variables. The first variable holds "Foo" and the second holds an instance of Qux with the Bar property set to "Baz" as desired. My Qux class looks like this:

namespace SomethingSimple.Models
{
   public class Qux
   {
      public string Bar { get; set; }
   }
}

Set up CORS in a modern Visual Studio 2015 MVC5 application which mixes MVC and the Web API.

  1. Go to: Tools > NuGet Package Manager > Package Manager Console
  2. Change the "Package source:" dropdown to be "nuget.org"
  3. Type in: Install-Package Microsoft.AspNet.Cors
  4. Put config.EnableCors(); in WebApiConfig which is a class found in the App_Start folder.
  5. Now slap [EnableCors(origins: "*", headers: "*", methods: "*")] on a Web API controller. It should work!

 
 

Addendum 12/20/2019: In modern times Install-Package Microsoft.AspNet.WebApi.Cors is probably the thing to use in step three above.

What's vbscript?

I don't know. I know what it's not however. This suggests that vbscript and VB.NET are super different to the point at which the difference is as extreme as Java and JavaScript which have nothing to do with each other. I guess I've never used vbscript and when I've said that I have I've mispoken.

Friday, December 11, 2015

pre-commit processes exist in Smart Bear Collaborator

In addition to code review codes of post-commits, there is also a way to generate code reviews off of Subversion Diffs and even tie in Collaborator to Subversion hooks so that one may not commit without a code review being done first. There is an actually desktop client for (at least) Windows beyond the web site for Collaborator and you have to use the client to do the pre-commits. A post-commit may be done with the client too even though I have been using the web site myself.

Thursday, December 10, 2015

You can't start a class name with a digit in C#.

This may lead to some stupid names like: ThreeDMovie

The zh-CN resources may be thought of as in Mandarin Chinese.

This says...

  • Mandarin and Cantonese are actually dialects
  • If something is written in simplified, it is most probably Mandarin, and most probably Mandarin as spoken in mainland China

Also, the Traditional and the Taiwan flavors of Chinese are BOTH of: zh-TW

Wednesday, December 9, 2015

Explicitly keep a button from submitting a form.

There are a couple of ways to do this:

  1. <button id="button" onclick="return false;">Go</button>
  2. <button id="button" type="button">Go</button>

 
 

I found the second approach of explicitly denoting a button type of "button" to be a little alarming. Here I had to explicitly go in another direction (there is also type="reset" which keeps the form from posting while clearing values out of form fields) and that begs the question: What is the default button type? This suggests a discrepancy between IE and other browsers and buttons in forms and those that are not so, and also suggests that one should always explicitly specify a button type at a button.