Tuesday, 23 February 2016

How to add version with html template URL in AngularJS?

Following is the code which you can use to add version with html template URL, please look into it.

Web.config file:   

 <appSettings>
        <add key="CURRENT_VERSION" value="4_0"/>
  </appSettings>

Index.cshtml file:


 var CurrentVersion = '@ViewBag.CurrentVersion';

app.js file:

 angular.module('app', [])
.config(['$httpProvider', function ($httpProvider) {

        $httpProvider.interceptors.push('httpInterceptorVersion');
     
    }])

    .factory('httpInterceptorVersion', function () {

        return {
            request: function (config) {

                if ((config.method === "GET") && (config.url.match(/\.\html?$/))) {
                    config.url += '?v=' + CurrentVersion;
                    // here you can target particular folder file

                    //if (config.url.indexOf("template/") !== 0) {
                    //    config.url += '?v=' + currentVersion;
                    //}
                }
                return config;
            }
        };
    });

I hope it might be helpful to manage caching issue.

   
 

Sunday, 1 March 2015

AngularJS: ng-include vs directive

It all depends on what you want from your code fragment. Personally, if the code doesn't have any logic, or doesn't even need a controller, then I go with ng-Include. I typically put large more "static" html fragments that I don't want cluttering up the view here.
If there is logic and a lot going on, then directives are the smarter choice (yet more daunting when you're new to AngularJS).

Ng-Include/data-ng-include

Sometimes you will see ng-include's that are affected by their exterior $scope / interface. Such as a large/complicated repeater let’s say. These 2 interfaces are tied together because of this. If something in the main $scope changes, you must alter / change your logic within your included partial.

Directives

On the other hand, directives can have explicit scopes / controllers / etc. So if you're thinking of a scenario where you'd have to reuse something multiple times, you can see how having its own scope connected would make life easier & less confusing.
Also, anytime you are going to be interacting with the DOM at all, you should use a directive. This makes it better for testing, and decouples these actions away from a controller / service / etc, which is something you want!
Tip: Make sure not to use restrict: 'E' if you care about IE8! There are ways around this, but they are annoying. Just make life easier and stick with attribute/etc. <div my-directive />

In conclusion:

·        You should be creating Directives a majority of the time.
·        More extensible
·        You can template and have your file externally (like ng-include)
·        You can choose to use the parent scope, or its own isolate scope within the directive.

·        Better re-use throughout your application

Tuesday, 24 February 2015

Angular model binding with MVC Html.TextBoxFOr

You can use ngModel with stronglytyped view as follows:

@Html.TextBoxFor(m => m.EmailAddress, new { @Value = "Email", @onfocus = "this.value = '';", @onblur = " if (this.value == '') { this.value = 'Email'; }", @class = "text", data_ng_model = "loginModel.EmailAddress" })


Sunday, 22 February 2015

Multiple types were found that match the controller named 'Home'.

Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.


This error message often happens when  you're trying to implement Areas in your project and you added two controller name with same inside area and the root. For example you have the two.

  • ~/Controllers/HomeController.cs
  • ~/Areas/Admin/Controllers/HomeController.cs
In order to resolve this issue(as the error message suggests you) you could use namespaces when you declaring your routes. So in main routes definition in Global.asax file you can use following line of code.


 routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "App.Web.Controllers" }
            );

and in your AdminAreaRegistration.cs

context.MapRoute( "Admin_default", "Admin/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional }, new[] { "App.Web.Areas.Admin.Controllers" } );

If you are not using areas it seems that your both applications are hosted inside the same ASP.NET application and conflicts occur because you have the same controllers defined in different namespaces. You will have to configure IIS to host those two as separate ASP.NET applications if you want to avoid such kind of conflicts. Ask your hosting provider for this if you don't have access to the server.

Sunday, 30 November 2014

FileLoadException was unhandled by user code(Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040))

I'm getting following error when I'm using NewtonSoft.Json with WebApi client. 

Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Solution: To everyone having problems with Newtonsoft.Json v4.5 version try using this in web.config or app.config:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json"
            publicKeyToken="30AD4FE6B2A6AEED" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>

  </runtime>

Thursday, 6 November 2014

Skip and Take record from 'N' to 'M' in SQL Sever

Hi Guys! In last weekend I was thinking let's write something on SQL Server database. So I was just thinking which topics should I covered or on which topics should I write...... thinking......thinking...., suddenly I got an idea lets write on some common questions or something which will more efficient during ours interviews or related to our project. So I've decided let's write on  "How to skip N to M records during table record selection?", I think this is very common question., So let's see how to do that in SQL Server database. 

Scenario: Suppose that, I've a table ABC and this table having following records,

Table: ABC
Id          Name       Address
1           Peter          XYZ
2           John           PQR
3           Scott B'u     CBT
4          Allen           123-TPO
5          Belly           986-PYT


So here question is Do we select 2 rows starting from 3rd row?

Solution:  I think the most elegant is to use the ROW_NUMBER function to resolve this problem.

WITH NumberedMyTable AS
(
    SELECT
        Id,
        Value,
        ROW_NUMBER() OVER (ORDER BY Id) AS RowNumber
    FROM
        MyTable
)
SELECT
    Id,
    Value
FROM
    NumberedMyTable
WHERE
    RowNumber BETWEEN @From AND @To
So in this way you can resolve this problem. Please feel free comment for 
any assistance or query.

11 Crazy World Records That Only Indians Could Have Held.

Indians are a competitive breed, which is proven by their obsession of breaking records. According to Guinness Indians rank 3rd in setting world records behind USA and UK and what’s interesting is the bizarre nature of these records. Here are a few world record set by Indians which cross the threshold of conventional.
For more information please follow the following link. http://www.rookiestew.com/11-crazy-world-records-that-only-indians-could-have-held/