Tuesday, October 30, 2018

ASP.NET MVC Exception : Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions


{InvalidOperationException: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions}

I recently encountered above exception, when I tried to bind Data Model to View to show count of Order and here is Code:



HTML View:

<div class="form-group">
            @Html.LabelFor(m => m.PostedOrders)
                <div class="col-md-3">
                    @Html.DisplayFor(m => m. PostedOrders.Count())
                </div>
</div>

If you see exception, it tell, you can only bind @Html.DisplayFor with Field, Property of Model and it does not support function or expression.



To fix this problem, you need to add one additional property or field in Model Class [OrderDataModel] that holds the total count of posted orders and you can bind this field/property with view to display total count of posted order

C# :

public class OrderDataModel
    {
        public List<Order> PostedOrder { get; set; }
        public int Total => PostedOrder.Count;
    }


HTML View:

<div class="form-group">
            @Html.LabelFor(m => m.Total)
                <div class="col-md-3">
                    @Html.DisplayFor(m => m.Total)
                </div>
</div>

Thanks for visiting, please leave your comments if it helps

Friday, October 26, 2018

GIT Vs TFS pros and cons

In this blog, we will discuss about GIT (distributed version source control) and TFS (centralized version source control) and about their the pros and cons.

GIT (distributed version source control) : 


GIT  Version Source Control
  • GIT is a distributed version control system and developer can clone the remote repository on dev machine including history.
  • After cloning of remote repository on local machine, you can commit your local changes into local branch without talking to remote repository and it give leverage to developer that he/she can work in off-line mode without talking to remote branch.
  • GIT allows developer to create as many as local private branch and after completion of work, he/she can quickly merge local branch to remote branch or switch context between one local private branch to another and can quickly delete local private branch.
  • GIT is only source version control system and it does not provide build automation and other release management feature but TFS do.


TFS (centralized version source control)    : 
  • TFS is a centralized version source control system.
  • No concept of local check-in in TFS and local work-space always connect to central repository.
  • No concept of local branch.
  • If TFS Server is unreachable or forgot to check-in your code, and it is risk to losing your work
  • TFS is bigger than source version control system, it provides many beneficial things- build automation and release management, Bug tracking and completely agile supported system

TFS Version Source Control

Thanks for visiting  and please leave your comments, if you feel something missing

Tuesday, October 23, 2018

WCF Exception : The message with Action 'http://tempuri.org ' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher


WCF Exception: The message with Action 'http://tempuri.org/IProduct/GetProduct ' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None)



There would be many reason for this exception and it can be :

·         Requested Method ‘GetProduct’ signature has been changed.
·         Requested Method ‘GetProduct’ has been deleted from WCF server.
·         Existing Binding Information has been modified or delete.
·         Binding security setting are not consistent between client and server

To resolve this contract mismatch issue, recreate or refresh the wcf client proxy, most proabaly this issue will be resolved.

Thanks for visiting!!

Monday, October 22, 2018

C# : Switch Case With When

In C#, Switch - Case statement is just alternative of IF-ELSE statement and it allows a variable to be equality compared against list of case values.



Example :
     string colorCode = "R";

            string Color = string.Empty;
            switch (colorCode)
            {
                case "R":
                    Color = "RED";
                    break;

                case "B":
                    Color = "BLUE";
                    break;

                case "Y":
                    Color = "YELLOW";
                    break;
                default:
                    Color = "UNKNOWN";
                    break;

            }
           
            Console.WriteLine(Color);
          
Output :
                RED  

but It is a very simple equality check , if you want to perform greater, less than or range pattern matching. It will not help you let’s see a real problem.

Problem:  Show student examination result based on student mark:

Mark < 32: Failed
Mark >=32 AND Mark <= 45: 3rd Grade
Mark > 45 AND Mark <= 60: 2nd Grade
Mark > 60: 1st Grade



In C# 7.0, C# introduce new Case-When Clause, where you can apply additional condition on that.

            int mark = 34;
            string ExamResult = string.Empty;
            switch (mark)
            {
                case int m when (m < 32):
                    ExamResult = "FAILED";
                    break;

               case int m when (m >= 32 && m <= 45):
                    ExamResult = "3rd Grade";
                    break;

                case int m when (m > 45 && m <= 60):
                    ExamResult = "2nd Grade";
                    break;

               case int m when (m > 60):
                    ExamResult = "1st Grade";
                    break;

                default:
                    ExamResult = "InValid";
                    break;

            }

                      Console.WriteLine(ExamResult);

Output :
                3rd Grade

Thanks for Visiting!!

Friday, October 19, 2018

ASP.NET WEB API : Invalid characters (>, <, *, space ) in WEB API URL

[HttpException]
at System.Web.Util.FileUtil.CheckSuspiciousPhysicalPath(String physicalPath)
at System.Web.CachedPathData.ValidatePath(String physicalPath)
at System.Web.HttpContext.ValidatePath()
at System.Web.HttpApplication.PipelineStepManager.ValidateHelper(HttpContext context)

I recently encountered the exception for requested WEB API URL, which contains invalid character on ending of URL and business object has validation rule to validation order ID format but instead of validation error message, I got resource is not available error message.

URL : https://localhost/api/OrderStatus/OrderID/234234%20

then I started investigation and found that on runtime, by default IIS validates each request URL, if it finds any invalid character, it throw exception



HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.

to disable the default url validation, i added and enabled the configuration setting ‘relaxedUrlToFileSystemMapping’ in web config. after this changes, i started getiing business validation error message – Input Parameter is invalid.

<system.web>
    <httpRuntime targetFramework="4.5.2" relaxedUrlToFileSystemMapping="true"  />   
  </system.web>

you can also include a list of invalid characters if request url contains any character which matches to given list, it throws exception, so in this way certain invalid you can allow to process it or for rest invalid character, you can stop and throw above exception

<httpRuntime targetFramework="4.5.2" relaxedUrlToFileSystemMapping="true"  requestPathInvalidCharacters="<,>,*,%,&,:,\,?" /> 

Thanks for Visiting!!

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...