ViewBag.Title “Object Reference Not Set”

It can be puzzling when you see something like the following getting a NullReferenceException, with “Object reference not set to an instance of an object.”

   1: @{

   2:     ViewBag.Title = "Report";

   3: }

The actual offending line of code is actually somewhere else in the view.  You probably need to try to go through a process of elimination by comment out section of the view code.

It is my case, the cause is the next time when I tried to assign to one of the properties of the model. The property was null, thus the error.

MVC Dynamic Appcache Manifest and Google Chrome “Application Cache Error event: Manifest changed during update”

For MVC, dynamically generate a manifest for appcache has another common problem:

Problem:

You have an AppManifestController and you have a view.

   1: @{

   2:     Layout = null;

   3:     HttpContext.Current.Response.ContentType = "text/cache-manifest";

   4:     

   5:     // must write out explicitly or else the first line will be empty, making the cache manifest not parseable by browser.

   6:     Response.Write("CACHE MANIFEST");

   7: }

   8: # Generated: @DateTime.Now.ToString()

   9:  

  10: ...

Google chrome will give the error message “Application Cache Error event: Manifest changed during update

Cause:

Google seems to get the appcache manifest twice.  The use of @DateTime.Now.ToString() means each request will get a slightly different manifest due to the different time.  And  Chrome chokes on that.

Solution

Just don’t do it; comment it out.  You don’t need that line.  If you are trying to expire the manifest, use other approaches.

   1: @{

   2:     Layout = null;

   3:     HttpContext.Current.Response.ContentType = "text/cache-manifest";

   4:     // must write out explicitly or else the first line will be empty, making the cache manifest not parseable by browser.

   5:     Response.Write("CACHE MANIFEST");

   6: }

   7: @*# Generated: @DateTime.Now.ToString()*@

MVC Dynamic Appcache Manifest and Google Chrome “Application Cache Error event: Failed to parse manifest”

For MVC, dynamically generate a manifest for appcache has one common problem:

Problem:

You have an AppManifestController and you have a view.

Google chrome will give the error message “Application Cache Error event: Failed to parse manifest

   1: @{

   2:     Layout = null;

   3:     HttpContext.Current.Response.ContentType = "text/cache-manifest";

   4: }

   5: CACHE MANIFEST

   6:  

   7: CACHE:

   8: https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js

 

Cause:

If you use Fiddler or just view source via browser, you will see the generated manifest has an empty first line instead of “CACHE MANIFEST”.  That screws things up.

Solution

You want to Response.Write() instead.

   1: @{

   2:     Layout = null;

   3:     HttpContext.Current.Response.ContentType = "text/cache-manifest";

   4:     // must write out explicitly or else the first line will be empty, making the cache manifest not parseable by browser.

   5:     Response.Write("CACHE MANIFEST");

   6: }

   7:  

   8: CACHE:

   9: https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js

JQuery DataTables

If you get the error “.DataTable is not a function”, you need to make sure you load jquery.js and jquery.dataTables.js first before you initialize the table with $(‘#sometable’).dataTable();

The second thing is to make sure you are not loading jQuery twice.  For example, your _Layout.cshtml may load a jquery and then you decide to load the latest jquery using google cdn.

The default DbConfiguration instance was used by the Entity Framework before the ‘SqlAzureConfiguration’ type was discovered

SqlAzureConfiguration is just a configuration class for SqlAzure.

   1: public class SqlAzureConfiguration : DbConfiguration

   2: {

   3:     public SqlAzureConfiguration()

   4:     {

   5:         SetExecutionStrategy("System.Data.SqlClient", () => new System.Data.Entity.SqlServer.SqlAzureExecutionStrategy());

   6:     }

   7: }

Problem:

I get the error when the app tries to instantiate the DbContext.

Solution:

   1: <entityFramework codeConfigurationType="WinWin.Domain.Model.SqlAzureConfiguration, WinWin.Domain.Model">

   2:     ...

   3: </entityFramework>