Monday, 30 March 2015

hide input rendered by editorformodel

The problem
We are using following code for login:

@using (Html.BeginForm())
{
    @Html.EditorForModel()
    <p>
        <button type="submit">Log In</button>
    </p>
}

The model is defined as:


 public class LogInModel
    {
        [Required]
        public string Email { get; set; }

        [Required]
        public string Password { get; set; }

        public string ReturnUrl { get; set; }
    }



So every time the page with the model renders, it will create 3 elements for user to imput data

One for email, second for password and the third for return url.

So how do we hide it?

We hide it using data annotation

 [ScaffoldColumn(false)]



so then our model will become:

    public class LogInModel
    {
        [Required]
        public string Email { get; set; }

        [Required]
        public string Password { get; set; }

        [ScaffoldColumn(false)]
        public string ReturnUrl { get; set; }
    }

Sunday, 29 March 2015

While writing my website I have read many blog posts how to do this the best way.

Couple targets I had in mind

  • Create bundle that will create smaller file to download
  • Almost everyone is using cdn, unless they are living behind firewall
  • And if cdn fails, provide file from local source

Key points:
  • Bundling is simply getting multiple files under one.
  • Minification is making files smaller
In my example I have java script files.

To render the bundle i need script in my page such as

 @Scripts.Render("~/bundles/jquery")

How to create a bundle


In your ASP.NET application you will have to find file: BundleConfig.cs
inside you will be able to find default setup, which might not be used in your template. 

     bundles.Add(new ScriptBundle("~/bundles/jquery")
                .Include("~/Scripts/jquery-{version}.js")); 

You might need to enable optimizations with code

 BundleTable.EnableOptimizations = true;
in order to see the efect on your page once run it.
   

Now to CDN


  1. content delivery network (CDN) is a system of distributed servers (network) that deliver webpages and other Web content to a user based on the geographic locations of the user, the origin of the webpage and a content delivery server.

Is supposed to offload some of the traffic, but what if I cannot access it?

We can create backup configuration, so I a case that cdn is unreachable we do not get down with our application.

How to achieve it?
First we need to tell bundle configuration that we are going to use cdn
  bundles.UseCdn = true;
And then we need to provide code with cdn first
  bundles.Add(new ScriptBundle("~/bundles/jquery", "https://code.jquery.com/jquery-{version}.min.js")
                .Include("~/Scripts/jquery-{version}.min.js"));
Notice the include after the path to cdn. This defacto says if you cannot get the file from here look to this alternative location

Example of the code is here:

One of many sources:
http://www.asp.net/mvc/overview/performance/bundling-and-minification
www.stackoverflow.com

Tuesday, 24 March 2015

xmlDoc.SelectSingleNode keeps same value in foreach loop


I have had an issue when looping through xml child nodes.

where using selector in loop kept same value.

xmlDoc.SelectSingleNode 

Example
var listOfNodes = xmlDoc.SelectSingleNode("//elementSelector");

foreach (XmlNode node in listOfNodes)
{
            var myValue = node.SelectSingleNode(@"//elementSelector").InnerText
}

myValue for each iteration returns same value even though the node changes


Solution is to add '.'

currentVenue.SelectSingleNode(@".//venueName").InnerText;


Now why this is:

The '.' in selector means to select the current node.
Without it, searching starts from the document root, not current element.

Difference between throw ex vs throw


Just had a 5 min before my VS finish building solution


Examples 

try {
DivByZero();
} catch (Exception ex){
throw;
}

try {
DivByZero();
} catch (Exception ex){
throw ex;
}



The difference is:

"throw ex"
  Resets the stack trace (so your errors would appear to originate from location where the code is throwing ex)
"throw"
 Keeps the original stack trace would be preserved.

Monday, 22 September 2014

Fast way of deleting large amounts of files

he worst way is to send to Recycle Bin: you still need to delete them. Next worst is shift+delete with Windows Explorer: it wastes loads of time checking the contents before starting deleting anything.
Next best is to use rmdir /s/q foldername from the command line. del /f/s/q foldername is good too, but it leaves behind the directory structure.
The best I've found is a two line batch file with a first pass to delete files and outputs to nul to avoid the overhead of writing to screen for every singe file. A second pass then cleans up the remaining directory structure:
del /f/s/q foldername > nul
rmdir /s/q foldername
This is nearly three times faster than a single rmdir, based on time tests with a Windows XP encrypted disk, deleting ~30GB/1,000,000 files/15,000 folders: rmdir takes ~2.5 hours, del+rmdir takes ~53 minutes. More info at Super User.
This is a regular task for me, so I usually move the stuff I need to delete to C:\stufftodelete and have those del+rmdir commands in a deletestuff.bat batch file. This is scheduled to run at night, but sometimes I need to run it during the day so the quicker the better.


Answer is taken from:
http://stackoverflow.com/questions/186737/whats-the-fastest-way-to-delete-a-large-folder-in-windows