Wednesday, July 20, 2016

0x800a1391 - JavaScript runtime error: '$' is undefined

Problem:

I had decided to upgrade to a newer version of bootstrap datepicker.  I had a new MVC project which started giving me this strange jquery error.


Solution:

The problem I ran into when getting this error was due to the fact that the following statements were not in the <head> of the document, but just before the </body> tag at the end of the document.
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryui")
@Scripts.Render("~/bundles/bootstrap")
Once I moved them to the <head> of my .cshtml file, everything worked fine.


Source:

http://stackoverflow.com/questions/17672643/0x800a1391-javascript-runtime-error-jquery-is-undefined-mvc-4

Tuesday, July 19, 2016

Customize .Net compilation directory.

Problem:

I had a vs project today which was building over another project.  The IIS express was referencing the older project each time I tried to run the new project.  What I needed to do was wipe the temp files.


Solution:

The dynamic created files for your web site are stored on

c:\WINDOWS\Microsoft.NET\Framework\version\Temporary ASP.NET Files\

You can change this directory on the web.config. The asp.net check if any file change on your site and if can find any changes is start the recompile. Also many parameters of the recompile can change on web.config.



<compilation  tempDirectory="" ...>






Source:

  1. http://stackoverflow.com/questions/12018403/where-is-the-compiled-dll-for-a-website-project
  2. https://msdn.microsoft.com/en-us/library/s10awwz0%28VS.100%29.aspx?f=255&MSPPError=-2147217396

Thursday, July 14, 2016

Keyset does not exist

Problem:

I have some code that makes a call to an external web service that is secured using X.509 certification.  If I call the service using the application pool set to Local service then it executes.


When I set the app pool to use a domain account domain\svc.account the webservice fails with the error: Keyset does not exist.


Solution:

This certificate is installed correctly but the domain account needs to be granted permissions to that certificate.  This will allow the service account to access the private key attached to the certificate.


  1. Start -> Run -> MMC
  2. File -> Add/Remove Snapin
  3. Add the Certificates Snap In
  4. Select Computer Account, then hit next
  5. Select Local Computer (the default), then click Finish
  6. On the left panel from Console Root, navigate to Certificates (Local Computer) -> Personal -> Certificates
  7. Your certificate will most likely be here.
  8. Right click on your certificate -> All Tasks -> Manage Private Keys



       9.Add you service account to the access list.  It will need a minimum of read permissions.




      10.It is possible that you may be required to add the local IIS_USRS group to the access list.  Grant it read permissions.

Source

Wednesday, July 6, 2016

Bootstrap 3 and IE11 clash. Flushing footer to bottom of the page, twitter bootstrap

Problem:


I was having issues with my site in IE 11.  Chrome and other browsers were doing fine.
The site navigation bars would not work with the vertical scroll.  The behavior was quite ugly.  The page would render correctly and the navigation menus would work fine.  As soon as the user scrolled the fixed horizontal navigation bar would spread all over the screen.  The only way to get the browser to redraw the screen was to resize the window.  This is hardly a state I can hand over a solution to a customer.:



Solution:

This was a footer and bootstrap problem.  First issue was that the main screen was using a fixed position reference which would not work properly in ie 11. 


{
  position:fixed;
}


The fixed property forced the footer to stay in place but the navigation bar would bleed into the main work area. 


Step 1: Removed the fixed position but then the footer started to move around. 


Step 2: The final fix was to use the bootstrap css class,navbar-fixed-bottom, this forced the footer to stay in place.


<div class="navbar-fixed-bottom">
...
</div>







Source:

  1. http://stackoverflow.com/questions/10099422/flushing-footer-to-bottom-of-the-page-twitter-bootstrap
  2. https://getbootstrap.com/examples/navbar-fixed-top/
  3. https://getbootstrap.com/components/#navbar
  4. http://getbootstrap.com/components/#navbar-fixed-bottom

Tuesday, July 5, 2016

MVC 5 Razor - Compilation Error CS0246 - Type or namespace could not be found


Problem:

I ran across this issue in a vs 13 project. I was running in my dev environment with a project I had reused. The project was building and running until I removed the web service reference. I followed the prescribed method in MSDN (see reference). Then each time I built the project it compiled. However, each debug session resulted in a runtime error.

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS0246: The type or namespace name 'ExpiredNamespace' could not be found (are you missing a using directive or an assembly reference?)





Solution:

I was absolutely puzzled by the error since I had removed all references to the namespace ExpiredNamespace from the project.  It was not reference anywhere.  If I was missing a reference then I should be seeing a compiler error. 


Then I began looking at the trace information and read up on the MVC razor process.  That is when I found my rogue reference.  The error always occurred when the View attempted to run.  I spared a look at the web.config.


<pages pageBaseType="System.Web.Mvc.WebViewPage">

<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="ExpiredNamespace'" />
</namespaces>

</pages>


The base page type renders the on run compilation using these classes.  I had inadvertently carried over the reference in the web.config from my old project!


Note to self, scrub your test projects carefully before reusing them.






Reference:

https://msdn.microsoft.com/en-us/library/d9w023sx.aspx

Friday, July 1, 2016

LinqKit predicate builder - Multiple WHERE Clauses with LINQ extension methods


// initial "false" condition just to start "OR" clause with
var predicate = PredicateBuilder.False<YourDataClass>();
if (condition1)
{
    predicate = predicate.Or(d => d.SomeStringProperty == "Tom");
}
if (condition2)
{
    predicate = predicate.Or(d => d.SomeStringProperty == "Alex");
}
if (condition3)
{
    predicate = predicate.And(d => d.SomeIntProperty >= 4);
}
return originalCollection.Where<YourDataClass>(predicate.Compile());



Predicate Builder class

using System;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
public static class PredicateBuilder
{
  public static Expression<Func<T, bool>> True<T> ()  { return f => true;  }
  public static Expression<Func<T, bool>> False<T> () { return f => false; }
  public static Expression<Func<T, bool>> Or<T> (this Expression<Func<T, bool>> expr1,
                                                      Expression<Func<T, bool>> expr2)
  {
    var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());
    return Expression.Lambda<Func<T, bool>>
          (Expression.OrElse (expr1.Body, invokedExpr), expr1.Parameters);
  }
  public static Expression<Func<T, bool>> And<T> (this Expression<Func<T, bool>> expr1,
                                                       Expression<Func<T, bool>> expr2)
  {
    var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());
    return Expression.Lambda<Func<T, bool>>
          (Expression.AndAlso (expr1.Body, invokedExpr), expr1.Parameters);
  }
}






Source:

http://www.albahari.com/nutshell/predicatebuilder.aspx
http://stackoverflow.com/questions/8791540/multiple-where-clauses-with-linq-extension-methods