: ASP.NET FAQ |
ASP.NET FAQ
Q: How do I start the
process of converting my ASP web pages/site to ASP.NET?
A: Microsoft® provides
documentation that contains information on how to convert from ASP to ASP.NET.
We recommend that you visit the following Microsoft pages for advice on how to
perform ASP-to-ASP.NET conversions:
Migrating ASP Pages to ASP.NET
Migrating ASP Pages to Web Forms Pages
In addition, we suggest that you
occasionally visit the
Microsoft Developer's Network home page and perform a search for asp.net.
Such searches will keep you up to date with the latest developments regarding
ASP.NET.
Q:
Why do I not see detailed error messages when I have an error in my ASP.NET
page? I only see information about a generic runtime error like the following:
Server Error in
'/appname' Application.
Runtime Error
Description: An application error occurred on the server. The current
custom error settings for this application prevent the details of the
application error from being viewed.
Details:
To enable the details of this specific error message to be viewable on the local
server machine, please create a <customerrors> tag within a "web.config"
configuration file located in the root directory of the current web application.
This <customerrors> tag should then have its "mode" attribute set to "RemoteOnly".
To enable the details to be viewable on remote machines, please set "mode" to
"Off".
|
<!-- Web.Config
Configuration File -->
<configuration>
<system.web>
<customErrors mode="RemoteOnly"/>
</system.web>
</configuration> |
Notes: The current error page you are seeing can be
replaced by a custom error page by modifying the "defaultRedirect" attribute of
the application's <customerrors> configuration tag to point to a custom error
page URL.
|
<!-- Web.Config
Configuration File -->
<configuration>
<system.web>
<customErrors mode="On" defaultRedirect="mycustompage.htm"/>
</system.web>
</configuration> |
A:
By default, ASP.NET applications are configured with custom
error messages turned off. However, some web development tools, such as Visual
Studio.NET, will create a web.config file with the customErrors mode set to “RemoteOnly”.
In order to enable detailed error messages, you need to make sure that you
either remove the customErrors section in your web.config file or that you set
the mode attribute in the customErrors section to “Off” in your site’s
web.config file.
<customerrors mode="off"/>
Be aware
that when you set the customErrors mode to “Off” all visitors of your web site
will see the detailed error message.
Note that due to the nature of the
shared hosting platform the “RemoteOnly” and “On” modes of the customErrors
section are equivalent since every visitor to your web site is browsing
remotely.
Q: Why do I receive an
error like the following when I attempt to browse my ASP.NET page?
Server
Error in '/appname' Application.
Configuration Error
Description: An error occurred during the processing
of a configuration file required to service this request. Please review the
specific error details below and modify your configuration file appropriately.
Parser
Error Message: It is an error to use a section
registered as allowDefinition='MachineOnly' beyond machine.config.
A:
You may have defined a section in your application’s
web.config file that is not configurable on our shared web hosting
platform. Remove or comment out any configuration sections from your
web.config file that are not supported. See
Supported Configuration Options for more information.
Q:
Which ASP.NET configuration options are supported in the ASP.NET implementation
on the shared web hosting platform?
A:
Many of the ASP.NET configuration options are not configurable at the site,
application or subdirectory level on the shared hosting platform. Certain
options can affect the security, performance and stability of the server and,
therefore cannot be changed. The following settings are the only ones that can
be changed in your site’s web.config file(s):
- browserCaps
- sessionState
- clientTarget
- pages
- customErrors
- globalization
- authorization
- authentication
- webControls
- webServices
- appSettings
See Microsoft's
ASP.NET Configuration for information on ASP.NET
Q: How do I
configure custom error messages for my .NET web applications?
A: Custom Error pages
can be configured in your web application's
web.config file in the <customerrors> section. Below is an example of a
custom error configuration:
<!-- Web.Config
Configuration File -->
<configuration>
<system.web>
<customErrors mode="On" defaultRedirect="error.aspx">
<error statusCode="404" redirect="404.aspx" />
</customErrors>
</system.web>
</configuration> |
With the above
configuration, any 404 (File Not Found) error will be result in a redirect to
the 404.aspx page. Any other error will cause the user to be redirected to the
error.aspx page. On our servers, using "On" and "RemoteOnly" for the
customErrors mode attribute will result in the same behavior since you will
always be viewing the site remotely. Setting the mode to "Off" will cause error
messages to be displayed directly to the user.
Please note that the
customErrors functionality will only work for ASP.NET pages. For example, an
unexpected error or a file not found error for a classic ASP file will display
the standard IIS error pages.
Q: What file
types are supported in ASP.NET?
A:
ASP.NET introduces several new
file types:
- Extension: .asax
- The Global.asax file, also
known as the ASP.NET application file, is an optional file that contains
code for responding to application-level events raised by ASP.NET. The
System.Web.HttpForbiddenHandler is used to prevent direct requests to this
file. This is the ASP.NET equivalent of the global.asa file.
- Extension: .ascx
- Used for custom User
Controls. When a page is executed with an ASCX reference, ASP.NET uses this
extension to process the request. The System.Web.HttpForbiddenHandler is
used to prevent direct requests to this file.
- Extension: .ashx
- Custom HttpHandler
implemented as a file rather than a separate class. HttpHandlers and
HttpModules are not currently supported on our platform.
- Extension: .asmx
- Files with these extensions
contain the WebService processing directive and serves as the addressable
entry point for XML Web services.
- Extension: .aspx
- Handles requests for ASP.NET
requests.
- Extension: .axd
- Used to handle Trace requests
(for debugging purposes). Tracing is not supported on our platform.
- Extension: .vsdisco
- XML Web Service dynamic
discovery document.
- Extension: .rem
- Requests for file names with
this extension are passed to the .NET remoting subsystem.
- Extension: .soap
- Requests for file names with
this extension are passed to the .NET remoting subsystem.
- Extension: .config
- The web.config file can be
used to configure many aspects of your web application. The
System.Web.HttpForbiddenHandler is used to prevent direct requests to this
file.
- Extension: .cs
- C# source file. The
System.Web.HttpForbiddenHandler is used to prevent direct requests to this
file.
- Extension: .csproj
- Visual C# (part of Visual
Studio .NET) project file. The System.Web.HttpForbiddenHandler is used to
prevent direct requests to this file.
- Extension: .vb
- VB.NET source file. The
System.Web.HttpForbiddenHandler is used to prevent direct requests to this
file.
- Extension: .vbproj
- VB.NET (part of Visual Studio
.NET) project file. The System.Web.HttpForbiddenHandler is used to prevent
direct requests to this file.
- Extension: .webinfo
- Usually contains the path to
the Visual Studio .NET project on the server. The
System.Web.HttpForbiddenHandler is used to prevent direct requests to this
file.
- Extension: .licx
- Text based files that are
used for licensing purposes. The System.Web.HttpForbiddenHandler is used to
prevent direct requests to this file.
- Extension: .resx
- XML based resource files. The
System.Web.HttpForbiddenHandler is used to prevent direct requests to this
file.
- Extension: .resources
- Similar to .resx files but
these types of resource file are created with the ResourceWriter class in
the .NET Framework. The System.Web.HttpForbiddenHandler is used to prevent
direct requests to this file.
Q: What is the
aspnet_client directory and what is it used for?
A: The aspnet_client
directory contains JavaScript files that are used by the ASP.NET runtime for
client-side validation functionality using the ASP.NET server validation
controls. This directory is created automatically when ASP.NET is enabled on
your site. This directory is not absolutely required for ASP.NET applications to
run. It is only required if you are using client-side validation with ASP.NET
server validation controls. However, we recommend you leave this directory
intact.
If you are using ASP.NET
validation controls with client-side validation enabled and your aspnet_client
directory is missing, your users may encounter errors.
Q: Are CodeBehind pages
supported?
A:
Yes, CodeBehind pages are supported in our
shared web hosting platform. For more information on CodeBehind and the ASP.NET
Web Form architecture, please see Microsoft's
Web Forms Code Model.
Q:
Where do I upload my .NET assemblies?
A:
Each ASP.NET application is automatically configured to
look in the \bin subdirectory, located immediately under your application root,
for the required .NET assemblies. By default, the root of your site is an
application. Remember, you can create more than one application on your site
using certain tools such as Visual Interdev or Visual Studio.NET. Each
application would need its own \bin directory. If you are using Visual
Studio.NET, your application (and its \bin directory) will be created
automatically when you create a new web project in a subdirectory of your site.
Q: How do I
compile my .NET web application?
A:
If all of your server-side code
is located within <script runat=server> tags in your .aspx files, all you need
to do is upload the files to the server and they will be compiled automatically
the first time they are execute. If you are using CodeBehind pages, you will
have to compile your source code files into a .NET assembly and upload it to the
appropriate \bin directory on the server. Using Visual Studio .NET is the
simplest way to accomplish this.
With Visual Studio.NET you can
develop your web application on your local machine and then use the Copy Project
feature (accessible from the VS.NET Project menu or from the Solution Explorer)
to deploy your application to your web site over the FrontPage Server
extensions. Please see your Visual Studio documentation for more information on
how to use this feature. Another option is to create a VS.NET web project
directly on the server and compile it. VS.NET will compile your project locally
and upload it to the server all in one step. Again, see your Visual Studio
documentation for more information.
If you do not have Visual
Studio.NET, you can use the command-line compilers included with the .NET
Framework SDK (download from
http://www.microsoft.com/net) to compile your assemblies. You would compile
them locally and then upload them to the appropriate \bin directory on the
server.
There are also other tools
available that can help you compile your .NET assemblies or with your .NET
development.
Q:
How is the global.asax file different from an ASP global.asa file?
A:
The global.asax file is very
similar to the global.asa file in classic asp. You can add a global.asax file to
the root of each web application on your site. The global.asax supports the
familiar Application_Start, Application_End, Session_Start, and Session_End
events. In addition, ASP.NET adds support for several new events. Please see
Microsoft's .NET documentation for more information.
Q: How should I
configure Session State in my web application?
A:
You can configure session state
by modifying the <sessionstate> element in your web.config file. There are
several options you can set in this section.
- mode (required)
- Off - use Off if you do not
use session state in your web application. This can improve the performance
of your web application.
- InProc - this tells ASP.NET
to store session state within the ASP.NET worker process(es). Please do
NOT use this setting if you want reliable
session state. This setting is incompatible with our configuration in the
shared hosting environment. Unfortunately, if you use Visual Studio .NET to
create your web.config, InProc is the default value and must be changed.
- StateServer - this tells
ASP.NET to store session state in an external process. This is the preferred
mode for session state in the shared hosting environment and is our system
default.
- SQLServer - we do not support
SQLServer for storing session state in the shared hosting environment.
- cookieless (optional)
- true or false (default) -
specifies whether sessions without cookies should be used. Cookieless
sessions can be especially useful if you need to support certain devices,
such as cell phones, that do not support cookies.
- timeout (optional)
- specify the time in minutes a
session can be idle before it is abandoned. The default is 20. We do not
recommend increasing this value.
- stateConnectionString
(optional)
- used to specify the server
name and port for the session State Server. If you are going to specify this
value, you should use "tcpip=127.0.0.1:42424". This is the default and you
are not required to specify this value.
- sqlConnectionString (optional)
- as mentioned above, SQLServer
session state mode is not supported so you should never need to specify this
attribute.
If you do not specify a <sessionState>
section in your web.config, your application will use the defaults defined on
the server. Below you can see the default configuration on the shared hosting
servers:
<sessionState mode="StateServer"
stateConnectionString="tcpip=127.0.0.1:42424" cookieless="false" timeout="20"
/>
Q: How do I send
an email message from my ASP.NET page?
A:
You can use the System.Web.Mail.MailMessage and the
System.Web.Mail.SmtpMail class to send email in your ASPX pages. Below is a
simple example of using this class to send mail in C# and VB.NET. In order to
send mail through our mail server, you would want to make sure to set the static
SmtpServer property of the SmtpMail class to
mail-fwd.
C#
<%@ Import
Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Mail" %>
<HTML>
<HEAD>
<title>Mail Test</title>
</HEAD>
<script language="C#" runat="server">
private void Page_Load(Object sender, EventArgs e)
{
try
{
MailMessage mailObj = new MailMessage();
mailObj.From = "sales@joeswidgets.com";
mailObj.To = "joesmith@someisp.com";
mailObj.Subject = "Your Widget Order";
mailObj.Body = "Your order was processed.";
mailObj.BodyFormat = MailFormat.Text;
SmtpMail.SmtpServer = "mail-fwd";
SmtpMail.Send(mailObj);
Response.Write("Mail sent successfully");
}
catch (Exception x)
{
Response.Write("Your message was not sent: " + x.Message);
}
}
</script>
<body>
<form id="mail_test" method="post" runat="server">
</form>
</body>
</HTML>
VB.NET
<%@ Import
Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Mail" %>
<HTML>
<HEAD>
<title>Mail Test</title>
</HEAD>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, E as EventArgs)
Try Dim Mailer As MailMessage
Mailer = New MailMessage()
Mailer.From = "sales@joeswidgets.com"
Mailer.To = "joesmith@someisp.com"
Mailer.Subject = "Your Widget Order"
Mailer.Body = "Your order was processed."
Mailer.BodyFormat = MailFormat.Text
SmtpMail.SmtpServer = "mail-fwd"
SmtpMail.Send(Mailer)
Response.Write("Mail sent successfully")
Catch ex As Exception
Response.Write("Your message was not sent: " + ex.Message)
End Try
End Sub
</script>
<body>
<form id="mail_test" method="post" runat="server">
</form>
</body>
</HTML>
Q: How do I
upload a file from my ASP.NET page?
A:
In order to perform file upload in your ASP.NET page, you
will need to use two classes: the System.Web.UI.HtmlControls.HtmlInputFile class
and the System.Web.HttpPostedFile class. The HtmlInputFile class represents and
HTML input control that the user will
use on the client side to select a file to upload. The HttpPostedFile class
represents the uploaded file and is obtained from the PostedFile property of the
HtmlInputFile class. In order to use the HtmlInputFile control, you need to add
the enctype attribute to your form tag as follows:
<form id="upload" method="post"
runat="server" enctype="multipart/form-data">
Also, remember that the /data
directory is the only directory with Write permissions enabled for the anonymous
user. Therefore, you will need to make sure that the your code uploads the file
to the /data directory or one of its subdirectories.
Below is a simple example of how
to upload a file via an ASP.NET page in C# and VB.NET.
C#
<%@ Import
Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<html>
<head>
<title>upload_cs</title>
</head>
<script language="C#" runat="server">
public void UploadFile(object sender, EventArgs e)
{
if (loFile.PostedFile != null)
{
try
{
string strFileName, strFileNamePath, strFileFolder;
strFileFolder = Context.Server.MapPath(@"data\");
strFileName = loFile.PostedFile.FileName;
strFileName = Path.GetFileName(strFileName);
strFileNamePath =
strFileFolder + strFileName;
loFile.PostedFile.SaveAs(strFileNamePath);
lblFileName.Text =
strFileName;
lblFileLength.Text = loFile.PostedFile.ContentLength.ToString();
lblFileType.Text = loFile.PostedFile.ContentType;
pnStatus.Visible = true;
}
catch (Exception x)
{
Label lblError = new Label();
lblError.ForeColor = Color.Red;
lblError.Text = "Exception occurred: " + x.Message;
lblError.Visible = true;
this.Controls.Add(lblError);
}
}
}
</script>
<body>
<form id="upload_cs"
method="post" runat="server" enctype="multipart/form-data">
<P>
<INPUT type="file" id="loFile" runat="server">
</P>
<P>
<asp:Button id="btnUpload" runat="server" Text=" Upload " OnClick="UploadFile"></asp:Button></P>
<P>
<asp:Panel id="pnStatus" runat="server" Visible="False">
<asp:Label id="lblFileName" Font-Bold="True" Runat="server"></asp:Label>
uploaded<BR>
<asp:Label id="lblFileLength" Runat="server"></asp:Label> bytes<BR>
<asp:Label id="lblFileType" Runat="server"></asp:Label>
</asp:Panel></P>
</form>
</body>
</html>
VB.NET
<%@ Import
Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<HTML>
<HEAD>
<title>upload</title>
</HEAD>
<script language="VB" runat="server"> Sub UploadFile(sender as Object, e as
EventArgs)
If Not (loFile.PostedFile Is Nothing) Then
Try
Dim strFileName, strFileNamePath, strFileFolder As String
strFileFolder =
Context.Server.MapPath("data\")
strFileName = loFile.PostedFile.FileName
strFileName = Path.GetFileName(strFileName)
strFileNamePath = strFileFolder + strFileName
loFile.PostedFile.SaveAs(strFileNamePath)
lblFileName.Text = strFileName
lblFileLength.Text = loFile.PostedFile.ContentLength.ToString()
lblFileType.Text = loFile.PostedFile.ContentType
pnStatus.Visible = True
Catch ex As Exception
Dim lblError As Label
lblError = New Label()
lblError.ForeColor = Color.Red
lblError.Text = "Exception occurred: " + ex.Message
lblError.Visible = True
Page.Controls.Add(lblError)
End Try
End If
End Sub
</script>
<body MS_POSITIONING="FlowLayout">
<form id="upload" method="post" encType="multipart/form-data" runat="server">
<P><INPUT id="loFile" type="file" name="loFile" runat="server">
</P>
<P><asp:button id="btnUpload" onclick="UploadFile" runat="server" Text=" Upload
"></asp:button></P> <P><asp:panel id="pnStatus" runat="server" Visible="False">
<asp:Label id="lblFileName" Runat="server" Font-Bold="True"></asp:Label> uploaded<BR>
<asp:Label id="lblFileLength" Runat="server"></asp:Label>bytes<BR>
<asp:Label id="lblFileType" Runat="server"></asp:Label></asp:panel></P>
</form>
</body>
</HTML>
|