Friday, July 22, 2016

Looking up values in Validation Rules

The VLookup function provides the ability to lookup values from other objects.  Note that it ONLY works on Custom Objects.  The format is: VLookup(field_to_return, field_on_lookup_object, lookup_value).

So if you need need to lookup an item from a custom table with an Id of 00Xj000000NK03j, and return the name, the formula would look something like this:  


Tuesday, July 19, 2016

Viewing the ValidationExceptions Collection

Occassionally, .NET apps that are using EntityFramework will throw the following error

System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
The easiest way to see what these exceptions is to add the following to the Watch window:
((System.Data.Entity.Validation.DbEntityValidationException)$exception).EntityValidationErrors


Wednesday, April 13, 2016

Creating an SSL Certificate for Azure App Services


  1. Generate the CSR (Certificate Signing Request).  An easy way to do that is https://www.digicert.com/easy-csr/openssl.htm
  2. Take the output of this page and run it in a Bash shell, or some place that you have access to the OpenSSL library.  This will create a csr file and a key file. 
  3. Buy the certificate.   These guys have good prices: https://www.ssls.com
  4. As part of the checkout, it will ask you to type in the text of the CSR file generated by OpenSSL in Step 2 above. 
  5. Not sure what to select when asked for Apache vs Windows.  But selected Windows, and I got a .cer file.  Support had to send me a .crt file instead. 
  6. After verifying who you are, then will send you a .crt file. 
  7. Use OpenSSL again to convert the .crt file and the .key file to a pfk
    openssl pkcs12 -export -out myserver.pfx -inkey myserver.key -in myserver.crt
  8. Upload the resulting .pfx file to the server.


  9.  


Some of this is taken from: https://azure.microsoft.com/en-us/documentation/articles/web-sites-configure-ssl-certificate/#bkmk_iismgr

Tuesday, November 24, 2015

WebAPI Headaches

WebAPI is great sometimes, and sometimes can cause you headaches.   I've found that using the format below as a starting point seems to resolve a lot of the problems.
  • Return an HTTPResponseMessage instead of a string.  Otherwise you will see an extra set of quotations around any string you return. 
  • Credentials should be pulled from the header instead of as a parameter on the method. 
  • Set the 'Content' property of the ResponseMessage to any string content you want to return.  If you want to return Json, set this value to 
  • Only use a single parameter on the methods you use. 
  • Use a DTO to pass more complex objects into a POST request. 


using System.Net
using System.Net.Http

 [HttpPost]  
 [Route("MyMethod")]  
 public IHttpActionResult MyMethod()  
 {  
     // check credentials  
     var user = Utility.AuthenticateUserFromHeader(Request.Headers);  
     if (user == null)  
         throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.Unauthorized));  
     var data = Request.Content.ReadAsStringAsync().Result;  
     // process the data   
     return Ok("Response message");  
 }  


Dealing with CORS
Put the following in the Web.config to help with CORS.  (If you like this part, go upvote this guys answer: http://stackoverflow.com/a/36901271/224531)

<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
  </customHeaders>
</httpProtocol>
If you have some extra HEADER parameter, you must add it to Access-Control-Allow-Headers, like:
<add name="Access-Control-Allow-Headers" value="Content-Type, X-Your-Extra-Header-Key" />
And finally, to handle OPTIONS requests you must reply with empty response, adding in your application class:
protected void Application_BeginRequest()
{
    if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")
    {
        Response.Flush();
    }
}

Monday, November 23, 2015

See 'EntityValidationErrors' property for more details

This is an annoying error.  It generally occurs when calling SaveContext in EF.  It means that there was something wrong with the data you were trying to Save, but it's not going to tell you what exactly.  

Fortunately, there is an easy fix if you are running in the debugger.  To get EF to cough up the EntityValidationErrors, simply create a new watch with the following:

((System.Data.Entity.Validation.DbEntityValidationException)$exception).EntityValidationErrors

Wednesday, January 14, 2015

Git: Pulling in tags from remote

When working with a Git repository, you may find that you don't have the latest set of tags, or that a tag has moved, and your local repository does not reflect the update.   To update the tags in your local repository to reflect the remote, do the following:
git fetch --tags
This will pull down all tags on the current branch.

Wednesday, December 31, 2014

Alert for Bootstrap

Do this if you want a Bootstrap alert at the top of your page.  Swap out 'alert-warning' for 'alert-error' if you want red instead of yellow.
 @if(Model.Message != null){  
      <div class="alert alert-warning">  
           <button type="button" class="close" data-dismiss="alert">&times;</button>  
           @Model.Message  
      </div>  
 }