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

Scheduled Task for .aspx (ASP.Net) file

Normally you may set the scheduled task easily by calling the program or script that you are using/coded, however if you come with the ASP.Net script, are you able to call through the asp dll library directly?

The answer is exactly no, as you may either use your Internet Browser to call your asp.net script, but your browser is going to open 1 session for each of your scheduled task ran.

With this article, Mick Genie will guide to use VBS script to call HTTP request to call your ASP.Net script.

1. First, create a .bat file from your notepad, insert cscript mickgenie.vbs and save it as mickgenie.bat.
2. Open another notepad and insert the line as below.
Call Send_HTTP_Request()
Sub Send_HTTP_Request()
On Error Resume Next
Dim objRequest
Dim URL
Set objRequest = CreateObject(“Microsoft.XMLHTTP”)
URL = “http://www.mickgenie.com/mickgenie.aspx” //replace your URL HERE
objRequest.open “POST”, URL , false
objRequest.Send
Set objRequest = Nothing
End Sub

3. Set the scheduled task from your box and test it.
4. There do not have step 4 🙂 .

Using ASP.Net MVC with different IIS

ASP.Net MVC is ASP.Net Modeling view controller where used to manage information and notify obeservers when that information changes.

To setting up ASP.Net MVC, you could setting it through IIS 6.0 or IIS 7.x.

  • IIS 7.x (integrated mode) – No special setting needed.
  • IIS 7.x (classic mode) – some setting needed to apply.
  • IIS 6.x or below – some setting needed to apply.

The setting needed as mentioned above actually is referring to creating the wildcard script map.

If you don’t want to use the URL mapping, you may actually ignore the setting as said, however if you want to use the mapping setting, you may refer as detail below.

IIS 7.0 with classic mode:

1.  Open IIS 7.0, select your application in the Connections window.
2. Make sure that the Features view is selected.
3. Double-click the Handler Mappings button.
4. Click the Add Wildcard Script Map link.

5. Enter the path to the aspnet_isapi.dll file (You can copy this path from the PageHandlerFactory script map)
6. Enter the name MVC.
7. Click the OK button.

IIS 6.0 or below:

1.  Right-click a website and select Properties.
2. Select the Home Directory tab.
3. Click the Configuration button.
4. Select the Mappings tab.
5. Click the Insert button.

6. Paste the path to the aspnet_isapi.dll into the Executable field (you can copy this path from the script map for .aspx files).
7. Uncheck the checkbox labeled Verify that file exists.
8. Click the OK button.