How to deploy an ASP. NET Website in IIS6 and 7

Source: Internet
Author: User

In my first blog, I published a sample project,
It mainly demonstrates my AJAX framework and my general data access layer.
Although I thought I had considered it more well and provided enough instructions,
However, in the nearly one year after the release, many people sent me emails asking me some questions about the failure to run examples.
Among all the problems, we mainly focus on the configuration of IIS and SQL SERVER.
Therefore, I think it is necessary to talk about the process of deploying ASP. NET websites in IIS6/7 and SQL SERVER.

In the previous blog [write your own ASP. net mvc Framework (below,
I released another example project, which also contains a website project,This example is the main process of introduction today.
The same part of the configurations required for the previous example will not be described again, and the differences will be added.

View the web. config file

ASP. NET websites are different from general desktop programs and can run without copying them (except for database connections ).
To run it, you usually need some configuration process.
But what exactly do we need to configure?
The answer is: view web. config.

Web. config is usually stored in the root directory of the website. This file contains the most important website running parameters. For example:
ConnectionStrings, httpHandlers, and httpModules are all parameters required by website developers to run the website.
Therefore, to deploy a website to IIS, you must first open the web. config file and check whether these important parameters meet the requirements one by one.

Some developers prefer to put all kinds of parameters in the appSettings configuration section. Even the database connection strings are stored in the appSettings.
I can only say:This is a bad habit.Therefore, when deploying such websites, you may need to pay attention to whether the appSettings contains database connection strings,
This requires manual identification. Of course, the deleettings may contain some important directory configurations, which can only be manually identified.

The parameters to be described today mainly involve IIS and SQL SERVER. Therefore, this article will introduce them separately.
In addition, IIS will be separately demonstrated in versions 6 and 7.5.
SQL SERVER is demonstrated in version 2005 Express.
The operating systems used for my demonstration are Windows Server 2003 and Windows 7, which are attached with IIS 6 and IIS 7.5, respectively.

Note: When deploying a website, we should first configure the website according to the important parameters defined in web. config.
However, in order to impress you with these parameters, in the following demonstration, the website is not configured based on web. config,
Instead, you can use [discover and solve problems step by step after creating a site] to explain this process.

Create a website in IIS

Every ASP. NET program is a website. To run them, you must deploy them in IIS,
The first step of deployment is to create a website in IIS.
The process of creating a website in IIS is relatively simple, because IIS provides a wizard interface to help us complete this configuration process,
Therefore, this article intends to omit those meaningless textures.

Description: we start with IIS6.
Here, you only need to create a website in IIS and configure it as a website. We will discuss other configurations later.
Sample project: Click here to go to The MyMVC DEMO Download Page

Have the website been created? Wait.

My website has been configured and can be accessed in a browser.

Now we can switch the style to try the effect. Click [3] in the upper-right corner of the page to try it.

Oh, why did you get an error at the beginning:

Don't be confused when you see this prompt. For the sake of user-friendly interface, I caught this error using JavaScript, but now we need to know the cause of the Error. What should I do?

Is there FireBug or Fiddler2?
If so, open them. It is the cause of the error I saw with FireBug (you need to re-execute the operation just now ):

From FireBug, we can see that the operation just triggered a request with the address/AjaxStyle/SetStyle. cspx
What is the cspx extension? Open web. config.

In web. config, in some httpHandlers that the website requires to configure, the first one is for the [cspx] extension.

Here I want to explain the [cspx] extension. In the previous example, I chose the [cs] extension to indicate an AJAX call,
However, it was found that many people encountered problems in IIS deployment (the reason will be discussed later ). So this time I changed the extension.
However, someone asked me:Cspx, is it wrong?Maybe he thinks it should be [aspx.

I have answered this question multiple times by email. Today I will reply it again in the form of a blog:
It doesn't matter what the extension name is. I just need to select an unused extension to differentiate AJAX calls.
I can also use [fish] as the extension of the AJAX request address, just worrying that some people think it is cool.

IIS6 add extension ing

Return to IIS, right-click the website node, select Properties from the pop-up menu, and select the Home Directory tab in the pop-up dialog box, click Configure.
At this time, the interface should be like this:

Find the extension [ascx] and double-click it. Will the following dialog box pop up?

Follow the image operation to complete: copy the setting path of the executable file.Click Cancel to close the dialog box.

Return to the application configuration dialog box, and click Add,

Follow the picture. The parameters of the executable file in the dialog box are already in the Windows clipboard. Now you only need to paste them.

Click OK to close all the dialog boxes, return to the browser, and try again.

Now you can operate it?

Do not click other links first. Let's make a summary of the operation just now.

In ASP. NET, sometimes we may need to create our own HttpHandler to process some special requests.
My MyMVC framework has this requirement: Separate AJAX requests from page requests for processing.
Therefore, we need URLs in special formats. It is usually easier to select a useless extension, so I chose cspx
To allow ASP. NET to map the following format of ULR to AjaxHandlerFactory

/Fish.AA.AjaxTest/Add.cspx/Fish.BB.AjaxTest.Add.cspx/Fish/BB/AjaxTest/Add.cspx/AjaxDemo/GetMd5.cspx/AjaxDemo.GetMd5.cspx

I need to register this URL mode in web. config, and in order to best match these URLs, I can use the following Configuration:

We can use WebDev. WebServer. EXE that comes with Visual Studio to run website programs.
Originally, it was perfect.
However, IIS does not support such complex path settings. It only supports simple extension ing.
Therefore, I have to use the simple extension [. cspx] to register with IIS.

Let me explain why in ASP. NET, the previous path can recognize the URLs in the five formats I mentioned above?
In the processor ing phase of the ASP. NET pipeline, ASP. NET converts [* Ajax */*. cspx, * Ajax *. *. cspx] to the following regular expression,
Then, check whether the URL of each request matches.

(?:\A|(?<=/))[^/]*Ajax[^/]*/[^/]*\.cspx\z|(?:\A|(?<=/))[^/]*Ajax[^/]*\.[^/]*\.cspx\z

If you are interested, you can also check that this regular expression matches the previous URL.
From this, we can also see that ASP. NET's ing implementation to the processor can support complicated URL modes.
For more information about the processor ing process, refer to my blog [refer to the HttpHandler ing process]

I explained why I finally chose [. cspx] to register a processor with IIS.
When registering, there are two more important parameters:
1. Executable File: it is actually a module that implements ISAPI. IIS will send the matching request to it and then hand it over to ASP. NET.
The path of this parameter is long, so we don't need to remember it. We just need to find an existing configuration and COPY it out.
2. Check whether the file exists:Do not select this parameter.Because the requested URL does not have a corresponding file.

Here we need to add one point:
In the FishWebLib demo I released earlier,
If you use the [cc] extension, you can follow the above method to register it.
But I also use the two extensions cs and ascx. Because the registration of these two extensions already exists,
Therefore, I need to modify their configurations: double-click the configuration items to make sure that you do not select [check whether the file exists.

IIS6 ing without extension

Let's go back to the previously configured example. The page should look like this:

Click the link "/mvc/Mers MERs" on the page to try.

Do not think that I set a trap on purpose. I did not expect this when designing the sample program.

Some people may think that there is another [path = "/mvc/*"] Not configured in httpHandlers?
Continue to follow the previous steps to solve the problem.

Well, I will try registering [path = "/mvc/*" again according to the previous method. The result is as follows:

Therefore, for such URLs with no extension,Is invalid.

For such URLs without extensions, you can add the wildcard application ing method in iis6.
Return to the application configuration dialog box and click Insert.... The following dialog box is displayed:

Set it according to the image. Then click OK to close the dialog box. The settings are as follows:

Click OK to close all the dialog boxes.

Let's go back to the sample program again. Now we can find that there are no problems.

If you use this method, you do not need to register cspx. At this time, all requests will be handed over to ASP. NET, and ASP. NET will identify the configuration I made in web. config.

Directory write permission

To facilitate the deployment process of MyMVC DEMO,
This time, I chose an XML file as the data source. The time for writing XML is when ASP. NET is stopped (Application_End event ).

To stop a website running in IIS, stop the [application pool] used by the website ].
You can find the application pool used by the website in the website Properties dialog box ]:

Switch to the application pool list in IIS, find the application pool in front, right-click,
Click properties from the pop-up menu, and then select the identification tab in the displayed dialog box:

From this picture, we can know which Windows account is running the website. Remember, this will be used later.

Okay,Click Cancel to close the dialog box.

To know which account the website runs, you can also view [Windows Task Manager ],
Find the process where w3wp.exe is located:

Back to the sample program, we can add some data at will.
Then, in the application pool list, stop the application pool used by the website. Start it again.
Refresh the page of the sample program.

What have you found? Is the data not saved?

If the data is not saved, continue reading.

The reason why data cannot be saved is not because the code is not executed, but because,The account running the website does not have the permission to write data files.
XML is stored in the App_Data directory of the website. to check whether the website has write permission, right-click the App_Data directory,
Select the properties menu and switch to the Security Tab:

After the previous analysis, we know that the website runs with a [network service] account,
However, in the security settings of this directory, the [network service] account is not allowed to write data,
Therefore, when a website stops running, data cannot be saved because it has no permissions.

In this case, we can grant the [network service] account the write permission to the App_Data directory.
The settings are as follows:

Now, you can restart the [application pool] Where your website is located to find that the data can be properly saved.

SQL SERVER Configuration

In the FishWebLib DEMO,
I introduced SQL SERVER to demonstrate my general data access layer,
Therefore, the sample program must be supported by SQL SERVER.

First, let's go back to web. config to see what kind of database the sample program needs to access:

Note: The SQL SERVER that the sample program needs to connect to is:Localhost \ sqlexpress

Three SQL SERVER instances are installed on my machine:

Therefore, I need to access it by using the named instance method.
If your machine uses SQL SERVER as the [Default instance] for installation, you need to change it:Localhost

In the compressed package in the example, I provide the data file required by SQL SERVER: db \ MyNorthwind. mdf
Before running the example, I need to [Attach] It to SQL SERVER.

Now you need to start SQL Server Management Studio, connect to the SQL SERVER instance,
In the tree control of object Resource Manager, find the database node, right-click it, and click the "add..." menu,
In the displayed dialog box, click Add..., select the MyNorthwind. mdf file, and click OK.

I have an error.

Take a closer look and find that the log file is not found.
Yes, I did not put the log file in the compressed package.
Okay, you can delete the log file in the dialog box and click OK again ].

Or error:

Note: This error is different from the previous one.
This time it means you do not have the write permission for the directory.

When there is no access to the directory, we first need to know which account the program is running.
This is very important. Otherwise, how do I configure Directory Access Permissions?

The easiest way to determine which account the program runs is to open [Windows Task Manager] and find the process.
Refer to the previous process for this method. At the end, we can find that sqlserver.exe is running with a [network service] account (my machine is like this ).
All right, follow the previous method to set the App_Data directory permission and then set the access permission for the directory where the MyNorthwind. mdf file is located.

Try to [Attach] The database again.
On my machine, you can now run the sample program.

Note: if you do not use the [Integrated Security = SSPI] connection authentication method for various reasons, you can also use the [user name/password] method,
Modify the connection string in web. config.

Summary:
1. Because SQL server data files are stored in Windows, you must grant the directory access permission required by the account that runs the SQL SERVER process.
2. When connecting to SQL server, it may also fail because SQL SERVER authenticates the connection identity. Therefore, you must configure the required access permissions.

So far, the IIS configuration and SQL SERVER Configuration required for the example are all described.

Deploy ASP. NET programs in IIS7

I have discussed how to deploy an ASP. NET Website in IIS6. Now I want to see how to complete this process in IIS7.5.
The next demonstration will take IIS7.5 of Windows 7 as the standard.

IIS7.5's improvement over IIS6 gives me the feeling that:Deploying an ASP. NET Website is too easy.

To be compatible with earlier versions, IIS7 supports two modes to run ASP. NET programs: integration mode and classic mode.
The so-called classic mode is actually to be compatible with the IIS6 mode. The following describes the integration mode to reflect the advantages of IIS improvement.

In IIS6, We need to configure the extension ing or wildcard ing, submit the request to an ISAPI filter, and then submit it to ASP. NET,
Finally, ASP. NET sends the request to our httpHandlers and httpModules.
The httpHandlers and httpModules we configured in web. config are invisible to IIS6. Therefore, they can only be configured in IIS again.

IIS supports running ASP. NET programs in a way called [integration mode] From IIS7. At this time, IIS can directly send requests to httpHandlers and httpModules of ASP. NET,
You can also directly read the configuration from web. config. Therefore, as long as you prepare web. config, the configuration task is very simple.

For this reason, I added IIS7 support in web. config in later examples.
The following section describes the IIS7 configurations required for the two sample projects.

FishWebLib DEMO only requires the following Configuration:

The configuration required for MyMVC DEMO is as follows:

Which of the above configurations can be viewed in IIS7.5?
See:

In this [processor ing] list, the first three are not exactly my configuration in web. config.

What is the configuration of fileExtensions?
See:

For FishWebLib DEMO, it uses extensions such as cs and ascx,
In addition, the two extensions are disabled according to the default configuration, so in that example, the purpose of this configuration is to change these two extensions into allowed access.

When I used IIS7.5 of Windows 7, I also found a different place than IIS6. It creates an independent application pool for each website by default, and the running account is not a NETWORK SERVICE,
However, we can easily modify it in the properties of the application pool.

Summary:
Deploying an ASP. NET Website in IIS7.5 is easy, provided that the system. webServer configuration section in web. config is prepared in advance.
Then you only need to create a website in IIS and point it to the program directory.
Note: If a program needs to access a local file or database, you also need to set the access permission for the file system or database. For details, refer to the relevant section above.

Port 80 and domain name

In the previous demonstration, port 25678 was selected for simplicity, because port 80 has been used.
In fact, this does not affect the operation of the website. However, the URL does not look beautiful.
Generally, HTTP uses port 80 by default. If this port is used, no port number will appear in the URL.
To make the URL look more beautiful, I will demonstrate how to use port 80.

In IIS, there are two methods to allow a website to run on port 80:
1. Use port 80 of another IP address for the website program.
2. Bind the specified domain name to the website.

You can add an IP address in the NIC configuration (if you already have multiple IP addresses, you do not need to do so ):

Then, in IIS, set [bind] For the website and use this IP address. See:

In addition to using the new IP address, we can also use the domain name to enable the website to run under port 80.
You can also set the website binding operation in IIS and specify a domain name for the website:

The final settings should look like the following:

Now we can use the following two methods to access my sample Website:

Http: // 192.168.0.222

Http://www.mymvc-demo.com

But here is another new question: where does the domain name come from?

There are two answers (for demonstration ):
1. Modify the C: \ Windows \ System32 \ drivers \ etc \ hosts file,
Add a ing entry:127.0.0.1 www.mymvc-demo.comYou can.
2. If you are using a Windows Server operating system, you can also assign a domain name to yourself. Please continue to read.

In the Windows Server operating system, we can use the DNS service to create our own domain name. The general process is:
1. Create a reverse search area.
2. Create a forward search area.
3. Create a WWW host in the forward search area.
4. Set binding for the website in IIS to point to the new domain name.
5. Set the DNS server address of the NIC to point to the machine with DNS service.
This method may be more difficult for a single machine than modifying the host file directly, but it is very convenient to use it within the LAN.

The DNS configuration process is as follows: (you can directly confirm the steps without textures in The Wizard)

Now we can use the domain name method to browse my example:

If you believe that reading this blog has some benefits, click 【Recommendation] Button.
If you want to discover my new blog more easily, click 【Follow Fish Li].
Because my enthusiasm for writing is inseparable from your support.

Thank you for reading this article. If you are interested in the content of my blog, please continue to follow up on my blog. I am Fish Li.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.