So Tired !_! 逆水行舟, 不进则退!

11Nov/10

直接把ASP.NET文件后缀改为php或者htm在IIS7上运行

Posted by Nick Xu

今天用了第三方开源的一个服务程序,返回页面写死了是PHP后缀的,因为不想修改别人的程序,只有我这边把ASP.NET的程序以PHP后缀来执行了.

问了谷哥好多次,都没有找到我想要的结果.后来想了一下,如果真正安装PHP的时候是怎样的呢?

然后我就查了一下PHP在IIS7下的安装过程(不是安装官方MSI包的那种方式哦),然后照版煮碗,结果,肯定是成功的啦,不然就不会写到这了,呵呵

我观察了一下设置完成后的WEB.CONFIG文件,其实在三个地方添加一点代码即可完成ASP.NET以PHP后缀运行,我直接把代码贴出来了.

<compilation debug="true">
<buildProviders>
<add extension=".php" type="System.Web.Compilation.PageBuildProvider" />
</buildProviders>
</compilation>
<httpHandlers>
<add path="*.php" verb="*" type="System.Web.UI.PageHandlerFactory" validate="true"/>
</httpHandlers>

<system.webServer>
<handlers>
<add name="php" path="*.php" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" preCondition="classicMode,runtimeVersionv2.0,bitness32" />
</handlers>
</system.webServer>

29Oct/10

Build ASP.NET/Mono Applications with mod_mono and Apache on Debian 5 (Lenny)

Posted by Nick Xu

mod_mono is an Apache module that makes it possible to run ASP.NET applications in Linux environments running Apache. While ASP.NET is a Microsoft technology and is traditionally used with IIS, mod_mono has become a viable option for deploying ASP.NET applications on Linux. This guide is inspired by the mod_mono guide created by the Ubuntu CommunityExternal Link and the Mono Project's Apache and Mono documentExternal Link with minor modifications. This guide does not cover installation and configuration of the Mono IDE which is used to develop ASP.NET applications on Linux. If you are interested in developing using Visual Studio for Mono, you can download a 30-day trial of the commercial Mono Tools plugin at the Mono Tools for Visual Studio pageExternal Link.

This guide assumes that you've followed the steps outlined in our getting started guide. You will install the Apache web server with very minimal configuration. If you already have Apache installed and configured, you may omit these steps; however, if you have not installed Apache and are unfamiliar with this server read the installation guide for additional documentation. Additionally, mod_mono is incompatible with the integrated PHP interpreter described in other guides. If you need to have both mod_mono and PHP running on the same Apache server you will need to run PHP scripts using the CGI method

These instructions work with the Linode platform. If you don't have a Linode yet, sign up for a Linux VPS and get started today.

Contents

Install Required Software Link

Before beginning the installation process, issue the following command to update your package lists:

apt-get update
apt-get upgrade

Install Apache Link

If you already have Apache installed and configured, you can safely skip this section of the guide. Install Apache by running the following command:

apt-get install apache2

As mentioned earlier, you will need to go to the installation guide if you wish to configure your server beyond the default configuration.

Install mod_mono Link

The Apache daemon must be stopped before mod_mono is installed. Issue the following command to stop the apache process:

/etc/init.d/apache2 stop

At this point we're able to install the required packages for mod_mono. Run the following command:

apt-get install mono-apache-server2 libapache2-mod-mono libmono-i18n2.0-cil

When the installation process completes start Apache with the following command:

/etc/init.d/apache2 start

Configure Apache Link

We recommend using name-based virtual hosts for web hosting. Refer to the Apache documentation for setting up Name-based virtual hosts.

Recent versions of mod_mono utilize the AutoHosting method of application deployment. This allows non-privileged users to deploy new applications without modifying Apache configuration files. While this provides great flexibility, it may also present a security risk. As a result, mod_mono must be enabled on a per-virtual host basis.

For the sake of this guide, we're going to create a site on the root of our example domain, ducklington.org. If you already have an Apache configuration for the root of your site, you will need to modify your existing virtual host file or create a new one on a subdomain of your site. Create the virtual host file, taking the following example virtual host configuration and modifying it to suit your needs. You may also use the Mod_Mono Configuration GeneratorExternal Link to generate your own custom configuration.

File excerpt: /etc/apache2/sites-available/ducklington.org

<VirtualHost *:80>
  ServerName ducklington.org
  ServerAdmin web-admin@ducklington.org
  ServerAlias www.ducklington.org
  DocumentRoot /srv/www/ducklington.org/public_html
  ErrorLog /srv/www/ducklington.org/logs/error.log
  CustomLog /srv/www/ducklington.org/logs/access.log combined

  MonoServerPath ducklington.org "/usr/bin/mod-mono-server2"
  MonoDebug ducklington.org true
  MonoSetEnv ducklington.org MONO_IOMAP=all
  MonoApplications ducklington.org "/:/srv/www/ducklington.org/public_html"
  <Location "/">
    Allow from all
    Order allow,deny
    MonoSetServerAlias ducklington.org
    SetHandler mono
    SetOutputFilter DEFLATE
    SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip dont-vary
  </Location>
  <IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript
  </IfModule>
</VirtualHost>

Save and close the file, and create the directories referenced in the DocumentRoot and ErrorLog directive:

mkdir -p /srv/www/ducklington.org/public_html
mkdir /srv/www/ducklington.org/logs

Enable the site by running the a2ensite command:

a2ensite ducklington.org

Since we have modified the virtual host configuration, Apache must be reloaded:

/etc/init.d/apache2 reload

Note: Should you restart Apache in the future, you will see an error that will look similar to this:

[crit] (13)Permission denied: Failed to attach to existing dashboard,
and removing dashboard file '/tmp/mod_mono_dashboard_XXGLOBAL_1' failed
(Operation not permitted). Further action impossible.

You can safely ignore this warning, as it won't affect deployment using the methods explained in this guide.

Installing MySQL Connector/Net for ASP.NET Link

This section assumes that you already have a functioning MySQL installation. Please refer to our MySQL Installation Guide for more detailed instructions for installing MySQL, otherwise issue the following command:

apt-get install mysql-server

In order for your ASP.NET application to communicate properly with your MySQL server, you must install the MySQL Connector/Net driver. The following commands download and install the 6.2.3 version of the connector. Check the MySQL UpstreamExternal Link to ensure that this the latest version of the plugin:

cd /opt/
wget http://dev.mysql.com/get/Downloads/Connector-Net/mysql-connector-net-6.2.3-noinstall.zip/from/http://mysql.mirrors.pair.com/
unzip -d mysqlConnector mysql-connector-net-6.2.3-noinstall.zip
cd mysqlConnector
gacutil -i mysql.data.dll
gacutil -i mysql.web.dll

Creating a Database to Test the MySQL Connector Link

Now that the MySQL Connector has been installed, you should test it by creating a sample database and a test table. First you must log in to your MySQL DBMS:

mysql -u root -p

Next you must create the sample table. Issue the following commands at the MySQL prompt:

CREATE DATABASE sample;
USE sample;
CREATE TABLE test (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(25));
INSERT INTO sample.test VALUES (null, 'Lucy');
INSERT INTO sample.test VALUES (null, 'Ivan');
INSERT INTO sample.test VALUES (null, 'Nicole');
INSERT INTO sample.test VALUES (null, 'Ursula');
INSERT INTO sample.test VALUES (null, 'Xavier');

Finally you must create a test user named "testuser" and give that user access to the newly created sample database:

CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'somepassword';
GRANT ALL PRIVILEGES ON sample.* TO 'testuser'@'localhost';
FLUSH PRIVILEGES;

Creating a Simple ASP.NET Application Link

Now that you have created a sample database, you can test your installation with the following test page. This will not only test your Mono installation but it will also will test your MySQL connector configuration. First create a file called testdb.aspx in your DocumentRoot and paste the text below into it. Be sure to change the User ID and Password to match what you specified above.

File excerpt: /srv/www/ducklington.org/public_html/testdb.aspx

[COLORIZE aspx-cs] <%@ Page Language="C#" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="MySql.Data.MySqlClient" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtdExternal Link"> <html xmlns="http://www.w3.org/1999/xhtmlExternal Link" xml:lang="en" lang="en"> <head> <title>ASP and MySQL Test Page</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script runat="server"> private void Page_Load(Object sender, EventArgs e) { string connectionString = "Server=127.0.0.1;Database=sample;User ID=testuser;Password=somepassword;Pooling=false;"; MySqlConnection dbcon = new MySqlConnection(connectionString); dbcon.Open();

MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM test", dbcon); DataSet ds = new DataSet(); adapter.Fill(ds, "result");

dbcon.Close(); dbcon = null;

SampleControl.DataSource = ds.Tables["result"]; SampleControl.DataBind(); } </script>

</head>

<body> <h1>Testing Sample Database</h1> <asp:DataGrid runat="server" id="SampleControl" /> </body>

</html> [/COLORIZ]

Next you will need to create a web.config file. You can copy and paste the the example below. Please note that Custom Errors have been turned off in this web.config for debugging purposes. The customErrors mode line should be removed in a production environment.

File excerpt: /srv/www/ducklington.org/public_html/web.config

<configuration>
        <system.web>
                <customErrors mode="Off"/>
                        <compilation>
                                <assemblies>
                        <add assembly="MySql.Data"/>
                                </assemblies>
                        </compilation>
        </system.web>
</configuration>

Visit the testdb.aspx file in a web browser. If you see the text "Testing Sample Databases" in your browser with the information that you inserted into the database above, you now have a functioning mod_mono installation and can continue with the development and deployment of your own application!

More Information Link

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

25May/10

在不同版本的IIS下使用ASP.NET MVC

Posted by Nick Xu

在这篇文章中我们学习在不同版本的IIS中使用ASP.NET MVC和URL Routing。我们学习针对IIS7.0、IIS6.0和更早版本的IIS的处理策略
ASP.NET MVC框架依赖于URL Routing,要充分利用URL Routing,我们需要对Web服务器(IIS)进行一些额外的配置。
IIS的最新版本是Windows2008中的IIS7.0,我们也可以Vista系统中(除了Home Basic版)安装IIS7.0。
IIS7.0是提供了两种处理请求的模式-集成模式传统模式如果使用IIS7.0的话,那我们不需要做任何配置,如果要使用传统模式的 话则需要我们做一些额外的配置工作。
在Windows2003中安装的是IIS6.0,如果是Windows Server 2003的话,那我们可以把IIS6.0升级到IIS7.0。但如果我们使用IIS 6.0的话,那需要我们做一些额外的配置工作。
Windows XP专业版安装的IIS5.1版本,因此我们也需要对IIS5.1进行一些额外的配置工作。
最后Windows2000使用的是IIS5.0,我们也需要对IIS5.0进行一些额外的配置工作。
下面是我们对不同版本IIS的总结:
IIS7.0(集成模式)- 不需要作任何配置就可以使用URL Routing。
IIS7.0(传统模式) - 需要我们进行特别配置来使用URL Routing。
IIS6.0或更低版本 - 需要我们进行特别配置来使用URL Routing。

一、集成模式与传统模式(原创:灰灰虫的家 http://hi.baidu.com/grayworm
IIS7.0可以使用两种模式来处理请求-集成模式和传统模式。集成模式提供更好、更多的功能;传统模式则是为了向后兼容较早版本的 IIS。
请求的处理模式是由程序池决定的我们可以通过指定程序池与应用程序关联的方式 来设置web应用程序的请求处理模式。步骤如下:
1. 运行IIS服务管理器
2. 在Connections窗口选择一个应用程序
3. 在Actions窗口中单击Basic Settings连接打开Edit Application对话框,如下图所示。
4. 设置Application pool。
默认情况下,IIS被配置为支持两种应用程序池:DefaultAppPoolClassic .NET AppPool。如果设为DefaultAppPool,那我们的应用程序 就运行在集成请求处理模式下。如果设置Classic .NET AppPool,那我们的应用程序就运行在传统的请求处理模式下。
《图1》
需要注意的是,我们通过点击Edit Application对话框中的”Select”按钮来改变程序池与应用程序的关联关系,来修改请求处理模式。但ASP.NET应用程序在从传统模式迁 移到集成模式的时候需要解决几个兼容性的问题。更多的信息请参见下列文章:
Upgrading ASP.NET 1.1 to IIS 7.0 on Windows Vista and Windows Server 2008 --http://learn.iis.net/page.aspx/270/upgrading-aspnet-11-to-iis7-on-windows-vista--windows-server-2008/
ASP.NET Integration With IIS 7.0 - http://learn.iis.net/page.aspx/243/aspnet-integration-with-iis7/
如果我们的ASP.NET应用程序使用的是DefaultAppPool,那我们不用做任何配置就可以使用URL Routing功能了。但如果ASP.NET程序被配置为Classic .NET AppPool,那请继续向下看。

二、在旧版本的IIS中使用ASP.NET MVC(原创:灰灰虫的家 http://hi.baidu.com/grayworm
如果我们使用的IIS版号比IIS7.0低或者使用的是IIS7.0传统模式,那我们有两种选择:
1、 修改路由表,加上文件扩展名。如我们把/Store/Details的URL地址换为/Store.aspx /Details
2、 创建wildcard script map。wildcard script map使我们把每个请求都映射到ASP.NET框架上。
如果我不能修改服务器的配置,那我们只好采用第一种方式,如果我们不想修改URL地址,那我们必须采用第二种方式,去配置IIS web服务器。

在这是我们分别讨论两种方法:
(一)向路由表中加扩展名。
要使URL Routing能够在旧版本IIS上运行的最简单的方法就是打开Global.asax文件,修改我们的路由表。路由表的代码如下:
Listing 1 – Global.asax (unmodified)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MvcAppCS
{
public class GlobalApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",// Route name
"{controller}/{action}/{id}",// URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
}
默认的路由配置可以对我们下面的ULR地址进行路由:
/Home/Index
/Product/Details/3
/Product
然而不幸的是,旧版本的IIS不会把这样的请求传递给ASP.NET框架,因此这些请求也不会被路由 到控制器。比如我们请求/Home/Index这个URL时,会产生一个错误页面的提示。如下图

《图2》
旧版的IIS只能把那些具有特定扩展名的URL请求发送给ASP.NET框架。比如/SomePage.aspx请求会被映射到ASP.NET框架,而 /SomePage.htm则不会被映射到ASP.NET框架。
因此,要想使URL Routing能够正常工作,我们必须要修改默认路由,使其包含文件扩展名,以映射到ASP.NET 框架中去。能 够映射到ASP.NET框架中的扩展名有.aspx .axd 和.ashx
修改后的Global.asax文件如下所示
Listing 2 – Global.asax (modified with extensions)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MvcAppCS
{
public class GlobalApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}.aspx/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = ""}// Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
}
注意:在修改完Global.asax文件后要记得重新编译我们的ASP.NET MVC应用程。
在上面的代码中,我们做了一个很小但很重要的修改,我们把默认路由修改为如下格式:
{controller}.aspx/{action}/{id}
由于这种更改,因此ASP.NET MVC应用程路由只能映射下面这种形式了
/Home.aspx/Index
/Product.aspx/Details/3
/Product.aspx
当我们修改完路由表后,我们还要确保程序中的所有超连接的URL地址也都已做相应的修改。换句话说,要保证所有的超链接导航地址中都包含.aspx扩展 名。如果我们使用Html.ActionLink()方法生成的超链接,那我们不需要对超链接做更改。

(二)创建Wildcard Script Map(原创:灰灰虫的家 http://hi.baidu.com/grayworm
如果我们不想修改ASP.NET MVC应用程序中的URL地址,并且可以访问到Web服务器,那我们可以通过创建 wildcard script map方式来把所有请求映射到ASP.NET框架中去。这样就避免修改默认的由表了。
要搞明的一点是,这种修改会使IIS对每个请求都会处理,那怕是请求一张图片、ASP页面、HTML页面。因此使用wildcar script map会使操作更隐式。

启用IIS7.0的wildcard script map
1. 在Connections窗口中选择我们的应用程序
2. 确定选中了Features视图。
3. 双击Handler Mappings按钮。
4. 单击Add Wildcard Script Map,如图。
5. 输入aspnet_isapi.dll文件的路径
6. 在Name文本框中输入MVC
7. 点击OK按钮。

《图3》

在IIS6.0中创建wildcar script map的步骤:
1、 右击站点,选择属性
2、 选择“Home Directory”选项卡
3、 点击“Configuration”按钮
4、 选择“Mappings”选项卡
5、 点击“Insert”按钮,如下图所云汉
6、 在Executeable文本框中输入aspnet_isapi.dll文件所在的路径
7、 去掉Verify that file exists复选框前的勾。
8、 点击OK按钮
《图4》

当我们配置完wildcard script map我们可以使用默认的路由表来处理下面的URL地址
/Home/Index
/Product/Details/3
/Product

总结

这篇文章中我们解释了如何在旧版本IIS(或IIS7.0传统模式下)运行ASP.NET MVC程。我们讨论了两种方式来解决URL Routing与旧版本IIS协同工作问题 :修改默认路由表或创建wildcard script map

第一种方法需要我们修改ASP.NET MVC应用程序,这种做法的好处是,我们不需要操作web服务器,而只是修改一下程序中的路由表即可。

第二种方法是需要我们创建wildcard script map,这种做法的好处是,我们不需要修改我们的代码,但缺点是会影响ASP.NET MVC程序的性能。

Tagged as: , , No Comments
   
site
site