Go Deploying Web Sites on IIS

Source: Internet
Author: User
Tags management studio sql server management sql server management studio

How to deploy an ASP. NET site in iis6,7

Read Catalogue

    • Begin
    • Viewing the Web. config file
    • To create a Web site in IIS
    • IIS6 Add extension mappings
    • IIS6 map with no extension
    • Write permissions for the directory
    • Configuration of SQL Server
    • Deploying an ASP. NET program in IIS7
    • 80 Ports and domain names

In my first blog, I published a sample project that mainly demonstrates my my AJAX framework and my generic data access layer. Although at the outset I thought I had considered more comprehensive, but also provided enough documentation, but after the release of nearly a year, still a lot of people sent me e-mail, asked me some questions about the inability to run the sample. In all of the issues, the main focus is on the configuration of IIS and SQL Server. Therefore, I think it is still necessary to talk about the process of deploying an ASP. NET site in IIS6/7 and SQL Server.

In the previous blog, "Write your own ASP. NET MVC framework (bottom)", I have released a sample project that also contains a Web site project, and today the introduction process will mainly be based on this example. the same parts of the configuration required for the previous example will not be repeated, and the differences will be supplemented.

Back to top view the Web. config file

Asp. NET Web site is different from a typical desktop program, which is not copied to run (except for database connections). To run it, you usually need some configuration procedures. But what exactly do we need to configure? The answer is: View Web. config

Web. config is usually placed at the root of the Web site, which contains one of the most important web site operating parameters. For example: Connectionstrings,httphandlers,httpmodules, these parameters are the parameters that the web developer deems necessary to run the site. Therefore, if we want to deploy a Web site to IIS, you must first open the Web. config file to confirm that these important parameters meet the requirements.

One thing to add: some developers prefer to put various parameters in the appsettings configuration section, even if the connection string for the database is placed in appsettings. I can only say: this is a very bad habit. Therefore, when deploying such sites, it may be important to note whether appsettings contains a connection string for the database, which requires manual identification. Of course, the appsettings may also contain some important directory configurations, as well as manual identification.

The parameters to be covered today are primarily IIS and SQL SERVER, so this article will cover them separately. And IIS is also divided into 6 and 7.5 two versions to demonstrate separately. SQL Server is shown in 2005 Express version. I am demonstrating that the operating system is: Windows Server 2003 and Windows 7, which are shipped with IIS 6 and IIS 7.5

Description: Typically, when you deploy a Web site, you should first configure the site based on those important parameters that are defined by Web. config. But in order for you to have a deeper impression of these parameters, the following demo does not first configure the Web site according to Web. config, but instead uses the "Create a site, step-by-step discovery problem and solve" way to explain the process.

Back to top Create Web site in IIS

Each ASP is a Web site, and if you want to run them, you need to deploy them in IIS, and the first step in deployment is to create a Web site in IIS. The process of creating a Web site in IIS is simple, because IIS provides a wizard interface to help us with this configuration process, so this article intends to omit those meaningless stickers.

Note, we start with IIS6 first. This only requires you to create a Web site in IIS, configure it as a Web site, and other configurations we'll talk about later. Example Project: Click here to enter MYMVC DEMO download page

Has the site been created yet? I'll stop for a moment.

My site is configured, and now it can be accessed with a browser.

Now we can switch style to try the effect, click on the top right corner of the page "3" try.

Oh, how it went wrong at first:

See this hint, don't be dazed. I caught this error with JavaScript for the sake of a friendly interface, but now we need to know what the cause of the error is, what to do?

Do you have Firebug or Fiddler2? If you have one, open them. is the cause of the error I saw with Firebug (need to re-execute the action):

From the Firebug, we can see that the operation just triggered a request, the address of the request is:/ajaxstyle/setstyle.cspx cspx what is an extension? or open the Web. config to see it.

<httphandlers> <AddPath="*.cspx "Verb="*"Type="Mymvc.ajaxhandlerfactory, Mymvc "Validate="True/> <AddPath="*.aspx "Verb="* "type=" validate=" true "/> <add path= "/mvc/*" verb= "* "type=" validate=" true "/></httphandlers >             

In Web. config, some of the httphandlers that the site requires to be configured, the first one is for the "cspx" extension.

Here I would like to explain the extension of "cspx". In the previous example, I chose the "CS" extension to represent an Ajax call, but later found that many people were having problems deploying in IIS (for the rest of the reason). So, this time I changed an extension. However, someone asked me:cspx, is not the wrong writing? Maybe he thought it was "aspx".

This question I answered many times with the mail, today in the form of a blog to answer again: take what extension is not important, I just choose not to use the extension to distinguish is the AJAX call can be. I can also take "fish" as an extension of the AJAX request address, just to worry about what some people think is tacky.

Back to top IIS6 add extension mappings

Back to IIS, on the site node, right-click, from the pop-up menu, select "Properties", and then in the popup dialog box, select the "Home Directory" tab and click on the "Configure" button. The interface should look like this at this point:

Find "ascx" this extension, double-click it, is not pop-up below this dialog box?

Follow the operation of the picture to complete: Copy the "executable" setting path. then click "Cancel" to close the dialog box.

This will go back to the Application configuration dialog, click on the "Add ..." button,

Follow the picture. The "executable" parameter in the dialog box, which is already in the Windows Clipboard, is now available as long as you paste it.

All OK, close all dialog boxes, go back to the browser, and try again.

Now you can do it, right?

Do not click on other links, or to do a summary of the operation just now.

In ASP., sometimes we might need to create the HttpHandler we need to handle some special requests. My MYMVC framework has this requirement: separate the AJAX request from the page's request. So we need some special format URLs. It is often convenient to choose a useless extension, so I chose cspx in order to tell ASP. ULR mapping to Ajaxhandlerfactory in the following format

/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 pattern in Web. config, and in order to best match these URLs, I can use the following configuration:

<httphandlers>    <path= "verb="type= "Validate="true "/></httphandlers>            

We can run the Web program with the WebDev.WebServer.EXE that comes with Visual Studio. Originally, this is all very perfect. However, this complex path setting is not supported in IIS, it can only support simple extension mappings. So, I had to use the simple extension: ". cspx" to register with IIS.

Let me explain why, in ASP., the previous path can identify the URLs of the 5 formats I mentioned above. During the processor mapping phase of the ASP. ASP, the net will convert "*ajax*/*.cspx,*ajax*.*.cspx" to the following regular expression, and then check that each request URL matches.

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

If you are interested, you can also check that the regular expression matches the previous URL. From here, we can also see that the implementation of the mapping of the processor by ASP. The mapping process for processors can refer to my blog, "HttpHandler mapping Process"

The previous explains why I finally chose ". Cspx" to register the processor with IIS. At the time of registration, there are also two parameters that are more important: 1. Executable: It is actually a module that implements the ISAPI, and IIS gives it the matching request and then gives it to ASP. The path of this parameter is longer, we don't need to remember it at all, just need to find an existing configuration, copy it out. 2. Verify that the file exists: This parameter must not be selected. because the URL we requested does not have a corresponding file exists.

One thing to add: In my previous release of the Fishweblib demo, a "cc" extension was used, which can be registered in the above method. But I also used the two extensions of "CS" and "ascx". Since the registration of these two extensions already exists, I need to modify their configuration: Double-click the configuration item and make sure that you do not check "Confirm file exists".

Back to top IIS6 no extension mappings

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

Click on the link "/mvc/customers" on the page to try it out.

Don't think I'm deliberately setting a trap oh. I didn't think of that at all when I was designing the sample program.

Here, some might think, there is not a "path=" in the httphandlers, "/mvc/*" is not configured? Continuing with the previous approach to configuration will solve the problem.

Well, I'll try to register the "path="/mvc/* "" in the previous way, and the results are as follows:

Therefore, the preceding method is not valid for this type of "no extension" url .

For URLs of this type without extensions, you can use the method of adding "wildcard Application Mappings" in IIS6 to resolve them. Go back to the Application Configuration dialog box and click on the "Insert ..." button to pop up the following dialog box:

Follow the picture to set it up. Then, OK closes the dialog box. The settings should look like this at this point:

Click OK to close all dialog boxes.

Let's go back to the sample program again, and we can see that there are no problems at all.

Add: If you use this method, the process of registering cspx in front of you is not required. Because all requests are now given to ASP. NET, and ASP. NET will recognize the configuration I made in Web. config.

Write permission to the top directory

In order to facilitate the MYMVC demo deployment process, I chose the XML file as the data source this time. The time to write XML is when ASP. NET is stopped (in the Application_End event).

A way to stop a Web site running in IIS is to stop the application pool used by the Web site. You can find the application pool used by the Web site in the Web Site Properties dialog box:

Then cut to the list of application pools in IIS, locate the application pool in front, right click on the "Properties" menu from the pop-up menu, and select the "Identity" tab in the dialog box that appears:

From this image, we can tell which Windows account the website is running on. Remember here, it will be used in the back.

OK, click "Cancel" to close the dialog box.

Add one thing: to know what account the site is running on, you can also view the Windows Task Manager to find the W3wp.exe process:

Back in the sample program, we can add some data. Then, in the application pool list, stop the application pool that the site uses. Start it again. Re-refresh the sample program's page.

What did you find out? Is the data not saved?

If you find that the data is not saved, you can continue reading.

The reason why the data cannot be saved is not because the code is not executed, but because the account that runs the site does not have permission to write to the data file. XML is placed in the App_Data directory of the Web site, in order to check whether the site has write permissions, you can right-click on the App_Data directory and choose the Properties menu to switch to the Security tab:

After the previous analysis, we already know that the site is running in the "Network Service" account, however, in the security settings of this directory, does not allow the "Network Service" account can have write access, so, when the site stops running, The data cannot be saved because there is no permission.

At this point, we can give the NETWORK SERVICE account write access to the App_Data directory. Settings such as:

Well, you can restart the "application pool" where the site is located, and you will find that the data is now stored properly.

Back to the top of SQL Server configuration

In Fishweblib demo, I introduced SQL Server to demonstrate my universal data access layer, so the sample program requires SQL Server support.

First, go back to Web. config to see what kind of Database the sample program needs to access:

Note: The SQL Server server that the sample program needs to connect to is:localhost\sqlexpress

There are three instances of SQL Server installed on my machine:

Therefore, I need to access it in a "named instance" manner. If your machine installs SQL Server as a "default instance", it needs to be modified to: localhost

In the sample compression package, I provided the data file required by SQL Server: Db\mynorthwind.mdf before running the sample, I need to "attach" it to SQL Server.

Now that you need to start SQL Server Management Studio, after connecting an instance of SQL Server, in the tree control of Object Explorer, locate the database node, right-click, and then click the "Attach ..." menu, and in the dialog box that appears, click "Add ... "button, select the Mynorthwind.mdf file, and then click the" OK "button.

I've made a mistake here.

Take a closer look and find that the log file was not found. Yes, I did not put the log file in the compressed package. OK, delete the log file in the dialog box, and then "OK" again.

or an error:

Note: This error is not the same as the previous error. This time it says there is no write permission to the directory.

When there is no access to the directory, we first need to know what account the program is running. this is very important. Otherwise, how do you configure access permissions for the directory?

The easiest way to determine which account your program is running is to open the Windows Task Manager and then go to the process. This method can refer to the previous procedure. Eventually we can see that Sqlserver.exe is running as a "NETWORK SERVICE" (My machine is like this). OK, then follow the previous method of setting App_Data directory permissions and then set the access permissions for the directory where the Mynorthwind.mdf file resides.

Try the "attach" database again and it will be completed successfully. On my machine, it is now possible to run the sample program.

Note: If for various reasons, do not use "Integrated Security=sspi" Connection authentication method, you can also use "username/password" way, then you need to modify the connection string in Web. config.

Summary: 1. Because SQL Server data files are saved in the Windows operating system, you must grant the directory access permissions that are necessary to run the account for the SQL Server process. 2. When you connect to SQL Server, you may also fail because SQL Server verifies the connection identity, and you must also configure the required access rights.

To this end, the IIS configuration required for the example and the configuration of SQL Server are covered.

Back to top deploy ASP. NET program in IIS7

Here's how to deploy an ASP. NET Web site in IIS6, and now look at how this process is done in IIS7.5. The next demo will take the IIS7.5 of Windows 7 as a standard.

IIS7.5 's improvement over IIS6, the feeling is that deploying an ASP. NET site is simply too easy.

IIS7 in order to be compatible with the older version, it supports two modes to run the ASP: Integrated mode, Classic mode. The so-called Classic mode, in fact, is to be compatible with IIS6 mode. The following will focus on integration patterns in order to demonstrate the benefits of IIS improvements.

In IIS6, we need to configure the extension mapping or wildcard mapping, hand over the request to an ISAPI filter, and then hand it over to ASP. ASP. Finally, the request is sent to our httphandlers, httpmodules. The httphandlers, httpmodules, which we configured in Web. config, is not visible to IIS6, so it can only be configured again in IIS.

Starting with IIS7, IIS supports running the ASP in a way known as "Integrated Mode", at which point IIS can directly submit the request to the httphandlers and httpmodules of ASP. The configuration can also be read directly from the Web. config, so as long as we have the Web. config ready, the configuration task is very simple.

For this reason, I have added support for IIS7 in the Web. config later in the example provided. The following is the configuration portion of the IIS7 required for the two sample projects.

The Fishweblib demo only needs the following configuration:

The configuration required for the MYMVC demo is as follows:

Which of these configurations will be reflected in the IIS7.5 where you see them? Please see:

In this "processor mapping" list, the top three are not exactly what I configured in Web. config.

What do you do with a fileextensions configuration in front of you? Please see:

For Fishweblib demo, it uses extensions such as CS, ascx, and these two extensions are forbidden by default, so in that example, the purpose of this configuration is to turn these two extensions into allow access.

When I was using Windows7 's IIS7.5, I also found a different place from IIS6, which would default to creating a separate application pool for each site, and running the account is not a network SERVICE, but we can easily modify it in the properties of the application pool.

Summary: Deploying an ASP. NET site in IIS7.5 is an easy task, provided that you have prepared the System.webserver configuration section in Web. config in advance. Then just create a Web site in IIS and point to the program directory. Note: If the program needs to access local files or databases, you also need to set the file system or database access rights, you can refer to the previous related subsections.

Back to top 80 port and domain name

In the previous demo, the 25678 port was selected for simplicity, because the 80 port was already in use. This does not actually affect the operation of the site, but the URL looks unattractive. HTTP is usually used by default port 80, if you use this port, then there will be no port number in the URL. To make the URL address look more beautiful, I'll show you how to use port 80.

In IIS, there are 2 ways to enable a Web site program to run in Port 80th: 1. Use the 80 port of the other IP address for the website program. 2. Specify the domain name bindings for the site.

We can add an IP address to the configuration of the NIC (if there are already multiple IPs, this is not necessary):

Then, in IIS, set the "bindings" for the Web site to use this IP address. Please refer to:

In addition to using a new IP address, we can use a domain name to allow the site to run under Port 80. method or set the bind action of the Web site in IIS, specifying a domain name for the site:

The final setting should look something like this:

At this point we can use the following two ways to access my sample website: http://192.168.0.222 http://www.mymvc-demo.com

But here's a new question: Where does the domain come from?

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

In the Windows server operating system, we can use the DNS service to create our own domain name, the approximate process is: 1. Creates a reverse lookup zone. 2. Create a forward lookup zone. 3. Create a new WWW host in the forward lookup zone. 4. Set the bindings for the Web site in IIS and point to the new domain name. 5. Set the DNS server address for the network card, pointing to the machine with the DNS service. This approach may be more cumbersome for a single machine than modifying the host file directly, but it can be very handy if it is used inside a LAN.

The related procedures for DNS configuration are as follows: (Steps without mapping in the wizard can be determined directly)

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

If you think reading this blog gives you something to gain, you might want to click on the " recommended " button in the lower right corner. If you want to find my new blog more easily, click Follow Fish Li in the lower-right corner. Because, my writing enthusiasm also inseparable from your affirmation support.

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.