Monday, January 6, 2014

URL.Action with a reference to another area in the project

Want to create a URL pointing to another area in your MVC application? Use

@Url.Action("Details", "Organisation", new {id = someid, Area = ""})

Area = "" is the root. Add you area....

Tuesday, June 26, 2012

Ajax loading images

Here's an awesome for creating and downloading loading images - http://ajaxload.info/

You can pick among several dozen styles, customize the background and download all for free.

Thursday, June 7, 2012

Internet explorer and AJAX caching headache in MVC

Spent several hours figuring our what was going wrong with my ajax calls. After the initial ajax call to a controller method that returns a JSONResult, the subsequent calls weren't even hitting the controller code. This was working fine in Chrome all along.

To fix this I had to create an ActionFilter attribute to specify no caching for httpgets. This post solved my problem - http://stackoverflow.com/questions/2027610/asp-net-mvc-2-rc-caching-problem

public class CacheControlAttribute : System.Web.Mvc.ActionFilterAttribute
    {
        public CacheControlAttribute(HttpCacheability cacheability)
        {
            this._cacheability = cacheability;
        }

        private HttpCacheability _cacheability;

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
            cache.SetCacheability(_cacheability);
        }
    } 

//in the controller
 [CacheControl(HttpCacheability.NoCache),HttpGet]
        public JsonResult GetUpcomingMatches(Guid id, int divisionId)
        {
            var reply = m_matchLogic.GetUpcomingMatches(id, divisionId);
            return Json(reply, JsonRequestBehavior.AllowGet);
        }

Wednesday, May 16, 2012

Javascript - Revealing Module pattern

Over the last couple of months I have grown to love structuring my JavaScript code using the "Revealing Module" pattern. I love the fact that it promotes the use of OO concepts in JS. With VS2011 adding more intellisense support to JS, it makes the use of this pattern so much better. Read more about it on Dan Wahlin's blog - http://weblogs.asp.net/dwahlin/archive/2011/08/02/techniques-strategies-and-patterns-for-structuring-javascript-code-revealing-module-pattern.aspx

Friday, May 11, 2012

Request for the permission of type 'System.Web.AspNetHostingPermission

I moved my asp.net app from a Win 2003 Server to a Win 2008 Server running IIS7 and I started getting Permission denied for "System.Web.AspNetHostingPermission" errors for my .ascx files.

Turns out that you have to set the "Load User Profile" for the specific application pool to True. That took care of the issue. 

Sql Express LocalDb

LocalDb - introduced around the Sql 2012 RC release is a geared toward developers and dev machines. Acting much like Sql Express simplifies development, without having to maintain a full blown Sql express instance. Localdb is spawned on demand via Visual studio without having to keep a SQL process/service running in the background all the time - adding a little more processing power to dev machines.

You can connect to the LocalDb instance like this - (LocalDb)\v11.0

Read more about LocalDb - http://blogs.msdn.com/b/sqlexpress/archive/2011/07/12/introducing-localdb-a-better-sql-express.aspx

Change default database name in EF 4.3 Code First

While using Code first in EF 4.3 the default generated db name will look like your namespace something.datacontext.

To change this behavior and use a custom name:

public class StockWatchDataContext:DbContext
    {
        public StockWatchDataContext(): base("StockWatch") //add your db name and override the base constructor
        { }

        public DbSet Security { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove();
        }
    }
Also make sure that you are not using localdb as the db server instance or you wont see that in SQL management studio.