Archives
-
How to 301 Permanent Redirect in ASP.NET
In the next release of the .NET Framework (.NET Framework 4.0) there is a new response method for permanently redirecting a request: Response.RedirectPermanent. You can see the Beta MSDN documentation for Response.RedirectPermanent here. This will automatically issue the 301 moved permanently status code and redirect to the target page. A permanent redirect status code tells a search engine to update their cache and reassign the old url to the new url.
But if you need to do this now (prior to .NET Framework 4.0) you will need to do it the manual way. To do so you will need to manually add the status and location headers to the response.
The pre .NET Framework 4.0 versions are:
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", "/");Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", "/");
Response.End();There is also a RedirectToRoutePermanent method which will allow you to redirect to a new url using route parameters (and sends the 301 Moved Permanently status code).
UPDATE: See RichardD's comment below for a different method. I have not tried it that way but I will next time because it certainly does look much cleaner.
Technorati Tags: C#,Permanent Redirects -
How to get an indexed item of an IEnumerable object (Linq)
If you have an IEnumerable<T> collection of some objects T and you need to get a particular item out of that collection, you cannot use the standard indexing that you would normally use with brackets ([index]). If you try you will get an error such as:
Cannot apply indexing with [] to an expression of type ‘System.Collections.Generic.IEnumerable<T>
But there is the extension method ElementAt(index) (in the System.Linq namespace) for IEnumerable<T> that will allow you to get at that particular indexed item:
MyItem = MyIEnumerableExpression.ElementAt(index);
From the MSDN Documentation: http://msdn.microsoft.com/en-us/library/bb299233.aspx
-
Batch file that monitors the number of files in a directory
Sometimes the fastest and easiest way to complete a task is a good old MS-DOS batch (.bat) file. A friend recently needed to get a quick fix in place for their production machine.
“I need to whip up a batch file that monitors the number of files in a directory. Basically, I want to see if there are 100 files in a directory and then start and stop a service to do some quick cleanup.”
The For command can be used to loop through the files in a folder and get a count and then do something based on that number.
@ECHO OFF
SETLOCAL
SETLOCAL ENABLEDELAYEDEXPANSIONSET count=0
for %%o IN (D:\temp\*.*) DO (
echo %%o
SET /A count=count + 1
)echo %count%
IF "%count%"=="100" ECHO NET STOP MyApp
ENDLOCAL ENABLEDELAYEDEXPANSION
ENDLOCALTechnorati Tags: Batch Files,MS-DOS -
How to prevent scrollbar jump in Firefox and Chrome
On Firefox and Chrome if the content of a page is not longer than the window, then the right scrollbar is not displayed, and likewise if the content of the page is longer than the window then the right scrollbar is displayed. This makes perfect sense except that for websites that are center justified (which is most websites these days) then the center content will appear to jump to the left when switching from a page with content that is shorter than the window to a page with content that is longer than the window. This jump can be a bit annoying since the header shift makes your eyes think something may have changed in the header (at least it does for me) so a rescan of the header occurs.
The best way to explain this is by an example. The Vaasnet home page is a short page with all of the content fitting within a single window (or above the fold):
Now switch to the Vaasnet Features page (which has content that is longer than the window or pushes below the fold) and the right scroll bar appears. This causes the entire page to shift to the left which looks somewhat not esthetically pleasing. It is hard to show in screen shots but it is much more apparent when browsing the site and the shift in the header and navigation is apparent and somewhat irritating.
There is a simple css fix to correct this problem. Just add the following style to your site:
html {overflow-y:scroll;}
Now the vertical scrollbar will always show on the right and you have eliminated the jump in the page content. (Notice the vertical scroll bar on the right hand side of the Vaasnet home page now.)
Technorati Tags: Scrollbar Jump in Firefox and Chrome,HTML Tips and Tricks -
HTML Tips and Tricks: Use div with border in place of hr
I am by no means an expert with html or css. Frequently I run into html or css issues that take me forever to get beyond. Recently I had an issue with a horizontal rule (<hr />) having extra padding in Internet Explorer. For some reason I could not figure out where the padding was coming from.
I don’t think this was an IE issue since a completely clean sample of html did not show the hr padding (or margin) problem in Internet Explorer. But for some reason the <hr> tag would not adhere to any padding or margin styling that I tried in Internet Explorer as it would in FireFox or Chrome.
After struggling with this for a while I realized I could use a <div> with a bottom border in place of the <hr>. It worked very nicely and gave me more control over the dividing line that I was trying to place in my user interface.
<div style="height:1px; width: 100%; border-bottom:solid 2px #ccc;" />
Maybe this will help any of the other HTML/CSS impaired developers out there.
Technorati Tags: HTML Tips and Tricks -
Excellent “What is Windows Azure?” Video
Technorati Tags: What is Windows Azure? -
Line-breaks and carriage returns (\r \n) are invalid in email subject
I received this exception when sending an email using System.Net.Mail.MailMessage:
"The specified string is not in the form required for a subject."
System.ArgumentException: The specified string is not in the form required for a subject.
at System.Net.Mail.Message.set_Subject(String value)
at System.Net.Mail.MailMessage.set_Subject(String value)The subject line of the email I was sending had a carriage return and line break in it so it made sense that this would be an invalid subject line.
And I was able to confirm that a check is made on the subject line to make sure there are not Carriage Returns or Line Feeds when the MailMessage.Subject property is set, but also that these are the only characters checked for.
To remove the line breaks and carriage returns I just did a simple find and replace:
subject= subject.Replace("\n", " ").Replace("\r", " ");
Technorati Tags: System.Net.Mail.MailMessage.Subject -
What is the difference between Hyper-V Manager Save and Pause?
The Hyper-V Management Console has several different actions you can take for a running virtual machine. Most are self-explanatory but I never quite new the difference between Save and Pause.
I always used Save to stop a virtual machine and save the current state of the virtual machine. But I did not understand how Pause would differ from Save. I finally went hunting for exactly what Pause does and found it in this Technet Article: Step 6 (Optional): Test Snapshots, Pausing, and Saving.
“You can also pause or save a virtual machine in a given state. When you pause or save a virtual machine, it stays in its current state for as long as you want.
Although pausing a virtual machine does not free up the memory that is allocated to that virtual machine, it frees up main processor resources. Saving a virtual machine frees up memory and main processor resources so that they can be used by other virtual machines or by the virtualization server.”So Pausing will hold the Virtual Machine in the current state in was in when you hit the Pause button and also keep it in RAM (continuing to use a valuable resource) but frees up the processor. Saving will also hold the virtual machine in the current state but it frees up the processor and RAM too.
So I think this would be true then:
Saving a virtual machine is to Hibernate as Pausing a virtual machine is to Sleep.