How to enable CORS in MVC?   Leave a comment

  1. Need to add Access-Control-Allow-Origin header value for each request that requires CORS support :

HttpContext.Response.AppendHeader(“Access-Control-Allow-Origin”, “*”);

2. Add this in the web.config file :

<system.webServer>

<httpProtocol>

<customHeaders>

<clear />

<add name=”Access-Control-Allow-Origin” value=”*” />

</customHeaders>

</httpProtocol>

</system.webServer>

We can follow either of the way.

Posted May 11, 2017 by Vasista Bhargav in ASP.NET MVC, c#

Issue : Azure cloud deployment fails : Certificate with thumbprint was not found   Leave a comment

What worked for me was:

  1. Goto powershell and type mmc
  2. Add certificates snap-in by going to File > Add/Remove Snap-in > Choose Certificates from the list > Choose My user Account
  3. Right click on Certificates – Current User and select Find Certificates
  4. On the dialog box, set Contains to ‘azure’ and Look in Field to ‘Issued To’
  5. Press Find Now. You should be able to see a list of certificates.
  6. Check for the thumbprint by double-clicking the certificate > Details tab > scroll down to Thumbprint
  7. Once you found your certificate, close the dialog, Right click and select Export
  8. Select to export the private key. Follow the steps until you have a *pfx file for upload to Azure
  9. Goto your service and select the Certificates tab
  10. Click Upload, select the exported *pfx file, and supply the password you’ve set during export
  11. Goto Dashbord and update the Cloud package
  12. List item

Credits : StackOverflow

Posted April 6, 2017 by Vasista Bhargav in Azure, c#

Issue : Error while publishing web role in VS 2013: There was an error reflecting type ‘Microsoft.Cct.AzureDiagnostics.ObjectModel.PublicConfig’   Leave a comment

  1. In your Azure project, edit any diagnostics.wadcfgx files in a text editor, and set <IsEnabled>false</IsEnabled> (near the bottom)
  2. Right click project > Publish, untick Send diagnostics data to Application Insights, hit Next

Credits : StackOverflow

Posted April 6, 2017 by Vasista Bhargav in Azure, c#

Check if a property exists in a dynamic variable   Leave a comment

using System.Linq.Expressions;

public bool IsPropertyExists(dynamic dynamicVariable,string propertyToCheck)
{
try
{
IDynamicMetaObjectProvider metaObjectProvider = dynamicVariable as IDynamicMetaObjectProvider;

if (null == metaObjectProvider) throw new InvalidOperationException(
“The supplied object must be a dynamic object ” +
“(i.e. it must implement IDynamicMetaObjectProvider)”
);

DynamicMetaObject dynamicMetaObject = metaObjectProvider.GetMetaObject(
Expression.Constant(metaObjectProvider)
);

IEnumerable<string> propertyNames = dynamicMetaObject.GetDynamicMemberNames();
bool exist = propertyNames.Contains(propertyToCheck);
return exist;
}
catch (RuntimeBinderException)
{
return false;
}
}

Usage :

bool propExists = IsPropertyExists(dynamicVariable, “Prop1”);

Posted February 9, 2017 by Vasista Bhargav in ASP.NET MVC, c#

How to insert a byte array into SQL Server varbinary column   Leave a comment

This can be done using Parameterised Queries as below :

string command = “Insert into [dbo].[Students] (Sname,BinaryColumn) Values (‘raees’,@binaryData)”;

SqlCommand sqlCommand = new SqlCommand(command, con);

sqlCommand.Parameters.AddWithValue(“binaryData”, [binary data]);

sqlCommand.ExecuteNonQuery();

 

Posted January 31, 2017 by Vasista Bhargav in c#, Database

Get the ASP.NET MVC Version Installed In Your System Using Code During Runtime   Leave a comment

string version = typeof(Controller).Assembly.GetName().Version.ToString();

Posted January 31, 2017 by Vasista Bhargav in ASP.NET MVC, c#

Simple Custom Jquery Datepicker Validator   1 comment

Pulgin :

(function($){
 var maxMonthValue = 12;
 var maxDayValue = 31;
 var noOfDaysInMonth = [0,31,28,31,30,31,30,31,31,30,31,30,31];
 var noOfDaysInMonthForLeapYear = [0,31,29,31,30,31,30,31,31,30,31,30,31];
 function AddDatePickerErrorMessage(id)
 {
     $('#'+id+'Validator').html('Please enter valid date').css('color','red');
 }
 function RemoveDatePickerErrorMessage(id)
 {
     $('#'+id+'Validator').empty();
 }
 function ValidateDateValues(value,id,dpFormat)
 {
     var leapYear = false;
     var getNoOfDaysInPresentMonth = 0;
     var monthValue = 0;
     var dayValue = 0;
     var yearValue = 0;
     if(dpFormat == 'dd-mm-yy')
     {
        monthValue = value[1];
        dayValue = value[0];
        yearValue = value[2];
      }
      else if(dpFormat == 'mm-dd-yy')
      {
         monthValue = value[0];
         dayValue = value[1];
         yearValue = value[2];
       }
       if((monthValue == "" || monthValue == null || monthValue == undefined)
          ||(dayValue == "" || dayValue == null || dayValue == undefined) 
          ||(yearValue == "" || yearValue == null || yearValue == undefined))
       {
          AddDatePickerErrorMessage(id);
       }
       else
       {
          if((monthValue.length > 2 || dayValue.length > 2 || yearValue.length != 4)
            || (parseInt(monthValue) > maxMonthValue || parseInt(dayValue) > maxDayValue)
            || (parseInt(monthValue) == 0 || parseInt(dayValue) == 0 || parseInt(yearValue) == 0))
          {
            AddDatePickerErrorMessage(id);
          }
          else
          {
             leapYear = LeapYearCheck(parseInt(yearValue));
             if(parseInt(monthValue) == 2)
             {
                if(leapYear)
                {
                   getNoOfDaysInPresentMonth = noOfDaysInMonthForLeapYear[parseInt(monthValue)];
                }
                else
                {
                   getNoOfDaysInPresentMonth = noOfDaysInMonth[parseInt(monthValue)];
                }
              }
              else
              {
                 getNoOfDaysInPresentMonth = noOfDaysInMonth[parseInt(monthValue)];
              }
              if(parseInt(dayValue) > getNoOfDaysInPresentMonth)
              {
                 AddDatePickerErrorMessage(id);
              }
          }
      }
 }
 function LeapYearCheck(year)
 {
   return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
 }
 $.fn.validateDatePicker = function()
 {
   this.each(function(){
   var $input = $(this)[0];
   $($input).keyup(function(){
      var dpId = $input.id;
      var dpFormat = $("#"+dpId).datepicker('option', 'dateFormat');
      var dpValue = $input.value;
      var completeDate = dpValue.split("-");
      if(dpValue != "")
      {
         if(completeDate.length == 3)
         {
            RemoveDatePickerErrorMessage(dpId);
            var monthInDate = 0;
            var dayInDate = 0;
            var yearInDate = 0;
            if(dpFormat == 'dd-mm-yy')
            {
               monthInDate = completeDate[1];
               dayInDate = completeDate[0];
               yearInDate = completeDate[2];
            }
            else if(dpFormat == 'mm-dd-yy')
            {
               monthInDate = completeDate[0];
               dayInDate = completeDate[1];
               yearInDate = completeDate[2];
            }
            var dateForValidation = [monthInDate,dayInDate,yearInDate];
            ValidateDateValues(dateForValidation,dpId,dpFormat);
          }
          else
          {
             AddDatePickerErrorMessage(dpId);
          }
       }
    })
 })
 }
})(jQuery);

Usage :

Html :

<input type="text" class="hasDatePicker" id="dp1" />
<div id="dp1Validator"><div>

Script :

$('.hasDatePicker').datepicker().validateDatePicker();

Posted October 16, 2016 by Vasista Bhargav in Html, Jquery/JavaScript