08 Dec 2014

AngularJS $http.post and OPTIONS verbs with C# MVC apps

development 2 Comments

If you have an angular service that posts data, the $http object will do a pre-flight OPTIONS request to the URL to make sure it can post.

1 $http.post('/someUrl', {msg:'hello word!'}). 2 success(function(data, status, headers, config) { 3 console.log(data); 4 }). 5 error(function(data, status, headers, config) { 6 console.log('error'); 7 });

On the flip side, don’t expect IIS to respect this.

In attempts to do this with AngularJs and the ionic framework, I was getting 404’s when calling post and nothing made any sense. Chrome debugging showed the OPTIONS call happening in Network.

After gnashing of teeth (aka googling) – I found the solution on Jef Claes’ blog.

Effectively, you have to obviously allow the OPTIONS verb, but there are two approaches he offers.

A granular, less elegant solution; handing it in controller. To do this, you’ll need to namespaces:

System.Net & System.Net.Http.

In my use case, I have a controller for various API requests, instead of a single controller for all API.

1 [AcceptVerbs("OPTIONS")] 2 public HttpResponseMessage ApiVerbName() 3 { 4 var resp = new HttpResponseMessage(HttpStatusCode.OK); 5 resp.Headers.Add("Access-Control-Allow-Origin", "*"); 6 resp.Headers.Add("Access-Control-Allow-Methods", "GET,DELETE"); 7 8 return resp; 9 }

Great, but then not so great, so the other option Jef details is a HTTP message handler.

In this approach, you create a new class, inherit from DelegatingHandler .  This is Jef’s example, exactly.

1 public class OptionsHttpMessageHandler : DelegatingHandler 2 { 3 protected override Task<HttpResponseMessage> SendAsync( 4 HttpRequestMessage request, CancellationToken cancellationToken) 5 { 6 if (request.Method == HttpMethod.Options) 7 { 8 var apiExplorer = GlobalConfiguration.Configuration.Services.GetApiExplorer(); 9 10 var controllerRequested = request.GetRouteData().Values["controller"] as string; 11 var supportedMethods = apiExplorer.ApiDescriptions 12 .Where(d => 13 { 14 var controller = d.ActionDescriptor.ControllerDescriptor.ControllerName; 15 return string.Equals( 16 controller, controllerRequested, StringComparison.OrdinalIgnoreCase); 17 }) 18 .Select(d => d.HttpMethod.Method) 19 .Distinct(); 20 21 if (!supportedMethods.Any()) 22 return Task.Factory.StartNew( 23 () => request.CreateResponse(HttpStatusCode.NotFound)); 24 25 return Task.Factory.StartNew(() => 26 { 27 var resp = new HttpResponseMessage(HttpStatusCode.OK); 28 resp.Headers.Add("Access-Control-Allow-Origin", "*"); 29 resp.Headers.Add( 30 "Access-Control-Allow-Methods", string.Join(",", supportedMethods)); 31 32 return resp; 33 }); 34 } 35 36 return base.SendAsync(request, cancellationToken); 37 } 38 }

Lastly – register it in the config:

GlobalConfiguration.Configuration.MessageHandlers.Add(new OptionsHttpMessageHandler());

 

I’m posting this so I won’t lose it and in hopes it is easier to find for someone else.

Good luck!

03 Dec 2014

An angularJs concept about ionic

development No Comments

Since I forget stuff…

It’s worth noting that when a template is rendered, the ng tags are processed at that time.

For example, if you want to use ng-model inside of an ng-repeat:

This does not work:

<input type=“text” ng-model=“settingObject.{{valueName}}”>

But this does:

<input type="text" ng-model="settingObject[valueName]">

This is an important concept because how certain element directives are processed have an impact on the resulting solution.

14 Nov 2014

My alternative to Net Neutrality–the municipal switched network

Casual Interest, development No Comments

There’s a lot of debate about Net Neutrality and many reasons for and against it. For that information, look elsewhere.  There’s a reason there’s a debate, of course. I would like to offer an outside the box alternative. This doesn’t have to be an either / or choice.  Let’s change the rules, not the rule makers.

What I’d like to share with the Internet is an alternative plan based around the OSI model.

In my model cities, towns, and counties build out switched networks to the curb. One of the challenges with competition is the physical space of where cables where be buried.  If one connection is already there, burying a second becomes very risky.

So, to address this, the municipal entity puts one in place.

In large data centers, they have what they call “meet me” rooms. Meet Me rooms are where the network within the datacenter actually connects to telephone companies, cable companies, and other ISPs.

In my model, each municipal would have a “meet me” location. Town hall. A school. A court house. It doesn’t matter. What matters is that the network congregates to that location.

When the network is build, a shared high speed connection is created for the community and connected to the network, bringing the network online.

Read more