Redirect domain without www to www with web.config

It is been long time user keep on asking how to redirect the domain name with www only from their website.

To get this fixed from IIS web.config file, you may copy the following script to your web.config file to redirect them from without www to www.

<rule name="www redirect" enabled="true" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^[^\.]+\.[^\.]+$" />
        <add input="{HTTPS}" pattern="off" />
    </conditions>
    <action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" />
</rule>
<rule name="www redirect https" enabled="true" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^[^\.]+\.[^\.]+$" />
        <add input="{HTTPS}" pattern="on" />
    </conditions>
    <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" />
</rule>

With the above code, it will redirect you without www to www even you have https configured.

Troubleshoot IIS7 Failed Request with Tracing

To enable Failed-Request Tracing from IIS 7, you may follow here,
1. Launch IIS7 (inetmgr in Windows Run.)
2. In the connections pane, expand the machine name, expand Sites and click on your domain entry such as mickgenie.com.
3. In the action pane, click on Failed Request Tracing.

4. In the Edit Web Site Failed Request Tracing Setting,click on the Enable checkbox  and press OK.
5. Now, go back to the site named mickgenie.com.
6. Click on the icon, Failed Request Tracing Rules.
7. In action pane, click Add.
8. In the Add Failed Request Tracing Rules wizard, select All content (*).
9. In the Define Trace Condition, define the status code such as 404.2, click Next.
10. Select the Provider, Verbosity and Areas then click Finish.

Enable detailed error in ASP and ASP.Net

With ASP and ASP.Net script, IIS is configure to disable any error for security concern. If you are in development stage, you could enable them from your web.config file without access to the server/control panel.

By fault, if you hit any error from your ASP/ASP.Net script, you shall receive error 500 Internal Server Error.

First of all, you should enable detailed error from your web.config file. To do this, enter the following command,
<system.webServer>
<httpErrors errorMode=”Detailed” />
</system.webServer>

Now, detailed error is enabled but you will found out the ASP/ASP.Net is still showing Error 500, Internal Server Error.To fix it,

For ASP.Net,
Find the system.web section or create it with,
<system.web>
<customErrors defaultRedirect=”errors.aspx” mode=”Off” />
</system.web>

For ASP,
Find the system.web section or create it with,
<system.web>
<asp scriptErrorSentToBrowser=”true” />
</system.web>

Redirect SSL Secured page from ASP.Net

As you know, you may easily force your site fully running SSL from your site but this will causing your site loading slowly as it is running encryption for your site.

Now, you may use the following script to apply your script to use SSL and you may apply this to your Page_Load function.

Dim strURL As String = Request.Url.ToString()
If Request.IsSecureConnection Then
    If strURL.IndexOf("http:") > -1 Then
    strURL = strURL.Replace("http:", "https:")
    Response.Redirect(strURL)
    End If
Else
    If strURL.IndexOf("https:") > -1 Then
    strURL = strURL.Replace("https:", "http:")
    Response.Redirect(strURL)
    End If
End If

Switch ASP.Net with 32 bits

Today, when I am trying to check the VPS client server and getting error of services unavailable all of the time.

As I do not know what is being installed earlier, hence I am checking it’s PHP and ASP.Net version. Finally, it is found out the problem came from the ASP.Net.

As the VPS is running Hyper-V with VPS, hence a 64 bits OS used. Hence changing the ASP.Net to 32 bits could get it fixed.

By changing the ASP.Net from 64 bits to 32 bits, you may refer to the step as below.
1. Open a command prompt.
2. Enable the 32 bits environment.

cscript %SYSTEMDRIVE%\inetpub\adminscripts\adsutil.vbs SET W3SVC/AppPools/Enable32bitAppOnWin64 1

3. Reapply the ASP.Net.

%SYSTEMROOT%\Microsoft.NET\Framework64\vx.xxx\aspnet_regiis.exe -i

where the x.xxx version is your .Net Framework version.

Get the server variable from ASP.Net

It is very often a developer or programmer want to know the system/server variable of a server that without their right to access to the server back-end.

With ASP.Net, you could get the server variable through scripting.

When you need these information from your IIS based server, create a aspx file named variable and place the script as below based on C# or VB.

VB

<% @ Page Language="VB" %>
<%
For Each var as String in Request.ServerVariables
Response.Write(var & " " & Request(var) & "<br>")
Next
%>

C#

<% @ Page Language="C#" %>
<%
foreach (string var in Request.ServerVariables)
{
  Response.Write(var + " " + Request[var] + "<br>");
}
%>

Well Done, hope this is useful for the programmer.

Understand ASP.Net custom error

It is very normal when you design your script in ASP.Net and getting some runtime error as attached below when you are designing your script from your local PC and uploaded to the web server.

With the above error, some programmer not even know what to do :S .

Today, MGe will guide you on what is the custom Error type and how to make it work when you are hosted to the web server that do not have the administrator privilege.

Basically custom error used to hide the real error from the script from the visitor, where it might be the sensitive information from your website such as connection string.

The ASP.Net <customErrors /> do have 3 types, such as On, Off and RemoteOnly. In short, may refer to the table as below.

Off Show everyone the detailed error message.  This is rarely a good idea.
On Don’t show anyone the detailed error message.
RemoteOnly Only show the detailed error message if you are testing from the local server where the site resides.

To set it, just open your web.config file and set it from the <system.web>.

Eg.

<customErrors mode="RemoteOnly" />

Calculate Folder size with ASP.Net

It is very important for a server space to be available time by time instead of always getting disk critical error from a server.

However, it is not possible for a server administrator to always log into the server to check the available space or used space for a folder.

Instead of using the server script to send an email to the administrator, user could use the ASP.Net script to show the used space.

VB Script:

Imports System
Imports System.Linq
Imports System.IO

Module Module1

Sub Main()
   Dim dInfo As New DirectoryInfo("C:/Articles")
   ' set bool parameter to false if you
   ' do not want to include subdirectories.
   Dim sizeOfDir As Long = DirectorySize(dInfo, True)

   Console.WriteLine("Directory size in MB : " & _
    "{0:N2} MB", (CDbl(sizeOfDir)) / (1024 * 1024))

   Console.ReadLine()
End Sub

Private Function DirectorySize(ByVal dInfo As DirectoryInfo, _
   ByVal includeSubDir As Boolean) As Long
   ' Enumerate all the files
   Dim totalSize As Long = dInfo.EnumerateFiles() _
     .Sum(Function(file) file.Length)

   ' If Subdirectories are to be included
   If includeSubDir Then
     ' Enumerate all sub-directories
     totalSize += dInfo.EnumerateDirectories() _
      .Sum(Function(dir) DirectorySize(dir, True))
   End If
   Return totalSize
End Function

End Module

Connect ASP.Net script with MySQL

It is very easy if you have ASP.Net script and want to connect to MySQL databases.

Today, Mick Genie will share how to connect your MySQL database with your ASP.Net script.

Sample Code:
==========
<%@ Page Language=”VB” debug=”true” %>
<%@ Import Namespace = “System.Data” %>
<%@ Import Namespace = “MySql.Data.MySqlClient” %>
<script language=”VB” runat=”server”>

Sub Page_Load(sender As Object, e As EventArgs)

Dim myConnection  As MySqlConnection
Dim myDataAdapter As MySqlDataAdapter
Dim myDataSet     As DataSet

Dim strSQL        As String
Dim iRecordCount  As Integer

myConnection = New MySqlConnection(“server=localhost; user id=mickgenie; password=password; database=database;”)

strSQL = “SELECT * FROM mytable;”

myDataAdapter = New MySqlDataAdapter(strSQL, myConnection)
myDataSet = New Dataset()
myDataAdapter.Fill(myDataSet, “mytable”)

MySQLDataGrid.DataSource = myDataSet
MySQLDataGrid.DataBind()

End Sub

</script>

<html>
<head>
<title>Simple MySQL Database Query</title>
</head>
<body>

<form runat=”server”>

<asp:DataGrid id=”MySQLDataGrid” runat=”server” />

</form>

</body>
</html>
==========

From the above code, you may change the setting from myConnection and you shall be able to connect without any problem.

Fixed DotNetPanel ASP.Net 4.0 not installed

Recently I found out when you have ASP.Net 4.0 installed from your server but your DotNetPanel do not show it is installed. To fix it, simply modify your MSSQL database data.

1. you should backup your MSSQL database.
2. Open table ServiceDefaultProperties.
3. Check for the ProviderID of 2 and 101 if contain AspNet40Path(32 bits server) or AspNet40x64Path(64 bits server).
4. If not, add them to the MSSQL and point to the path as below.
AspNet40Path -> %SYSTEMROOT%\Microsoft.NET\Framework\v4.0.30128\aspnet_isapi.dll
AspNet40x64Path -> %WINDIR%\Microsoft.NET\Framework64\v4.0.30128\aspnet_isapi.dll

Check if ASP.Net 4.0 show from your server setting. If not, you may proceed to the step below.

1. Backup MSSQL database.
2. Open table ServiceProperties.
3. Check if AspNet40Path and AspNet40x64Path existed for each of the ServiceID.
4. If you have multiple web server and do not know which server belong to which ServiceID, open table Services and Servers.
5. Check for Services table and look for ProviderID of 2(IIS6) or 101(IIS7). Check for the ServerID and verify the server from Servers table.
6. Add the record as below.
AspNet40Path -> %SYSTEMROOT%\Microsoft.NET\Framework\v4.0.30128\aspnet_isapi.dll
AspNet40x64Path -> %WINDIR%\Microsoft.NET\Framework64\v4.0.30128\aspnet_isapi.dll