2006年10月19日

VS 2005 Web Deployment Projects

Later this week Microsoft is shipping on the web a new free add-on to Visual Studio that enables what we call “Visual Studio 2005 Web Deployment Projects”. This add-on provides a lot of additional deployment features and capabilities that complement the built-in web project support in the product.  The below blog post provides a quick overview of some of the new things coming. There are also some MSDN whitepapers that will document it more exhaustively over the next week as well.

 

Some background

 

The web project model changed in a number of ways between VS 2003 and VS 2005. There was a lot of discussion this August over missing features that weren’t supported or were broken by it in Beta2 (examples: exclude file from project support, file-based assembly refreshes, the ability to not check-in \bin assemblies in source control, etc). These specific issues were fixed in the final release of VS 2005, and a number of folks who previously had a lot of concerns have posted positive impressions of the new model lately.

 

One of the things we on the team still weren’t happy with, though, was the level of support in the vanilla product for more advanced deployment configurations/options beyond the core "Publish Web" scenarios built-into VS 2005 – specifically around assembly naming/output, advanced MSBuild customization, and other common scenarios that we’ve heard requests for over the years (for example: the ability to modify connection string settings depending on the build configuration, etc).

 

A couple of guys on my team have cranked over the last 2 months adding richer support for these scenarios that integrate nicely in the shipping product. We’ve packaged up this work in a new free download we call “VS 2005 Web Deployment Projects” and which we worked to make sure was available for download this week (which is the week of VS 2005 launch -- so basically immediately when the product releases). This download does not touch any shipping binaries within VS 2005, but instead uses the extensibility support within the IDE and ASP.NET to integrate in a fairly cool way. 

 

We think the resulting feature-set offers the best of both worlds – specifically the productivity benefits of rapid development brought with the new dynamic web project model, as well as the richness of a highly customized build/deployment system.

 

VS 2005 Web Deployment Project Feature-Set

 

The VS 2005 Web Deployment Project download adds a number of features that integrate nicely inside VS 2005 including:

 

1) More control over the number of assemblies generated by a pre-compiled web project, as well as control over their naming. Specifically, you can now generate a single named assembly for all pages + classes in your web project (for example: MyCompany.MyWebApp.dll). Alternatively, you can also now optionally generate a separate named assembly for each directory of pages/controls in your project (for example: MyCompany.MyWebApp.SubFolder1.dll and MyCompany.MyWebApp.SubFolder2.dll). This later option is particularly useful for large content web-apps where you would like the ability to incrementally patch code updates.

 

2) The ability to utilize the full power of MSBuild to customize your build process. You can now use MSBuild to define pre and post build rules, exclude folders from building, automatically create IIS vdirs and site mappings, add custom assembly versioning information, and add any custom MSBuild task you want to your build process.

 

A VS 2005 Web Deployment Project stores all of its settings inside an MSBuild based project-file. It also adds support for opening and editing the web deployment MSbuild project file directly in the IDE (w/ full intellisense support for it) – something that actually isn’t supported by any other project type in VS.

 

3) The ability to define and use custom build-configurations inside Visual Studio, and define per-build configuration options. In addition to using the built-in “debug” and “release” configurations, for example, you could define custom build configurations like “staging”. You can then vary your web deployment MSBuild tasks and actions based on these.

 

4) The ability to customize and modify a web application’s web.config file at deployment. There is now IDE support for changing values likes connectionstrings or app-settings as part of the build process. You can also vary these values based on the VS build configuration – so for example you could have one set of connectionstrings that you use for development (when you have the build configuration set to “debug”), and then have them modified as part of the build-process to your test/staging or production databases when you choose a “staging” or “release” build configuration in the IDE.

 

There are also a host of other features to take advantage of. The upcoming whitepapers on MSDN will go into more detail on these, as well as the 40+ new web deployment related MSBuild tasks that it ships with that you can use to customize things even further.

 

Example Walkthrough of using a VS 2005 Web Deployment Project

 

Below is a simple example that walks-through using the VS 2005 Web Deployment Project download to customize the deployment of a VS 2005 based web site project.

 

Step 1: Development my web project

 

Below is a screen-shot of a common web application solution structure – where I have 1 or more class library projects and then a web-project containing my web app.

 

 

I can build the application using the standard web project model that ships with VS 2005, and take advantage of the productivity enhancements it provides.

 

Step 2: Adding a VS 2005 Web Deployment Project

 

As my web app progresses, I’ll need to think more about deployment. To take advantage of the new deployment features the web deployment add-in provides, I can right-click on any of my web projects in the solution and add a Web Deployment Project (note: it is the 3rd item in the context menu below):

 

 

This will prompt me for what I want to name the project and where to store its project file:

 

 

The web deployment project will then show up as part of my solution:

 

 

Associated in the e:\WebSiteDeployment directory is a new MSbuild based project file that contains deployment and build settings for my web-project.

 

Note that a single web-site project can optionally have multiple deployment projects associated with it. You can also have multiple web-site projects in a solution, each with a separate web deployment project (for example: a user control library project that you pre-compiled and then copied into another web app project that used it).

 

Step 3: Setting Build and Deployment Options

 

To customize build and deployment settings I can either use the built-in GUI property pages for the deployment project, or edit the MSBuild file directly. I’ll show this second more advanced option later – for right now I can just right-click and pull up the property pages for the project, or double click on the WebSiteDeployment node (which is a short-cut for the same thing) to customize its properties.

 

There are several screens of information (the MSDN whitepaper will go into them all – for here I’ll just show a few highlights). One thing to note is that the settings are configured per-VS build configuration – so you can have one set of options for “debug”, one set of options for “release” and optionally define your own build configurations like “staging” and configure custom options for that build configuration as well. I’m going to change the settings below to apply for the “release” build configuration.

 

Here are a few of the more common things developers will want to control:

 

1) Developers can control whether to pre-compile the web entirely, or to support html source updates. Only the later option was supported in VS 2003 – and meant that you always had to deploy your .aspx source files on your production server (these were in turn dynamically compiled by ASP.NET at runtime the first time the app was run). ASP.NET 2.0 still supports this compile option (we call it the “allow updates” option in VS 2005). ASP.NET 2.0 also now supports a mode where you can optionally compile everything inside VS – including the .aspx/.ascx control definitions and html source. This produces a more efficient version of the app, and eliminates the need for a first-time dynamic compile performance hit when your app is run. It also allows you to protect more of the IP of your site, and enables you better compile-time checking of your application. The first property page in the web deployment project allows you to choose either this new compile mode, or the preserve .aspx source mode that VS 2003 used.

 

2) Developers can control what granularity of assembly output they want the website compiled down to, as well as what names to use for the assemblies (this is controlled on the output assemblies tab):

 

 

The default option is to merge the entire web-site into a single assembly that you can name however you want. This merges the output of all page/control compilation, app_code, and web-services into a single assembly (leaving one file to deploy). 

 

There are other options you can choose as well for more granular control over the assembly generation. For example, the second option above lets you compile each folder in your website into a separate named assembly (for example: MyCompany.MyWebApp.Folder1, MyCompany.MyWebApp.Folder2). This is useful for patching large applications where you want to make a tactical update to just a portion of the site.

 

If you have an AssemblyInfo.cs/.vb class in your app_code directory, the deployment project will use the meta-data it contains to version the produced assemblies. Alternatively, you can also set the version information specifically in the IDE. This in turn saves it as an MSBuild variable in your project file. You could optionally use a custom MSbuild task to set this variable dynamically at build-time (for example: to use a policy like we do at Microsoft where the build number is based on the current date).

 

3) The developer can then optionally use the deployment tab for other common deployment scenarios:

 

 

One of these is the ability to replace the application’s web.config settings at build-time. This can be done on a configuration section by section basis. Simply list the name of the section to replace, and an XML file containing the replacement settings. You can choose to have them replaced either in-line within the web.config file (the default), or to use the new configSource attribute option in ASP.NET 2.0 to dynamically point the web.config file at external configuration files that contain the values.

 

Because all values in the settings dialog can be configured on a per-VS build configuration basis, this means that you can have one set of configuration file replacement settings per build configuration (for example: one connectionstring setting for “debug”, one connectionstring setting for “staging” and one connectionstring setting for “release”). 

 

The other options included on this dialog is the ability to dynamically create and map an IIS vroot to the output directory as part of the build process, as well as the ability to delete the app_data directory as a post-build step (useful in cases where you want to delete the SQL Express database you’ve been using for development, and instead map your Membership, Roles, Profile, Personalization and HealthMon providers to a full SQL Server production database).

 

Step 4: Adding Optional Advanced MSBuild File Customization Rules

 

The above GUI property pages provide support for the most common build configuration settings. One neat thing about the Web Deployment Projects, though, is that there is easy IDE support for dropping down into the MSBuild file directly and customizing things further.

 

To-do this in the IDE, simply right click on the web deployment project and select “Open Project File”:

 

 

This will bring up an XML editor with intellisense support for the project file:

 

 

You can make changes, and then hit save to update things dynamically.

 

The web deployment project adds support for pre and post build events that you can use to customize things. For example, if you wanted to make sure a custom directory was created in your generated app after building, you could use the built-in MSbuild <MakeDir> task in the AfterBuild event:

 

 <Target Name="AfterBuild">

    <MakeDir Directories="$(TargetDir)\Upload" />

 </Target>

 

If you want to exclude a “Test” directory that contains some sample test pages from building in your release configuration you could add something like this:

 

  <ItemGroup>

    <ExcludeFromBuild Include="$(SourceWebPhysicalPath)\Test\**\*.*"/>

 </ItemGroup>

 

Note that you can use wild-card extensions when excluding things (above it is using *.* to exclude everything under the test directory) – so you can get quite fancy with your exclude rules. MSBuild itself ships with tons and tons of built-in build tasks (like MakeDir). The Web Deployment Project download adds support for another 40+ custom tasks specific to web application development/deployment. The really cool thing is that it is also easy to build your own MSBuild tasks and add them to projects as well.

 

Once you’ve made any custom tweaks, just hit save on the project file and close the window. Note that you can still use the property page dialogs above even after customizing the project file – it will preserve your settings and not blow them away.

 

Step 5: Building the Web Deployment Project

 

Now that I have my deployment project all setup, I can simply do a “Build Solution” to build all the projects in my solution – including my new web deployment project. This will then generate an output folder for my website. Contained within it is a \bin directory that has two assemblies (one for my data class library, and one for my web project):

 

 

The web.config file for my application has also been updated to have my production connectionstring settings.

 

One typical optimization that we’ll recommend would be to only build the web deployment project when your application is ready to be deployed – and not during actual development (instead when actively developing just work with the web project directly as normal). You can easily configure this in the VS build configuration manager:

 

 

For example: with the above settings we are configuring the WebSiteDeployment project to not build when the “debug” release configuration is set – and only do so when in “release” mode.

 

You can then use the build configuration drop-down on the toolbar to quickly switch between debug and release modes depending on where you are with the project:

 

 

Step 6: Optional Setup Project

 

One last bonus step that is also possible is adding a “Web Setup” project into our above solution. This is a standard VS 2005 project type that supports packaging a .msi file along with logic to create a vroot and deploy an application when executed:

 

 

I can easily configure the WebSetup project to pipe in the output from my WebSiteDeployment project into the MSI. 

 

Then, while I select “Build Solution” again I’ll build my web deployment project, pipe it into the setup project, and I’ll be left with a .MSI setup program for my web application that will walk me through creating an IIS vroot and deploy my production app on a web-server when run:

 

 

Next Steps

 

Our plan is to post the first release of the VS 2005 Web Deployment Project for free download on MSDN this week.  We'll then gather and incorporate feedback and release an updated version later this year. 

 

We hope you find it useful. Let us know what you think and what additional features and MSBuild tasks you’d like to see added to it.

 

Thanks,

 

Scott

 

posted @ 2006-10-19 08:16 kary 阅读(574) 评论(0) 编辑

2006年6月6日

GridView绑定数据源,无记录时表头也显示的一种解决方法

转:Show Header/Footer of Gridview with Empty Data Source
public void BuildNoRecords(GridView gridView, DataSet ds)
{
try {
if (ds.Tables(0).Rows.Count == 0) {
ds.Tables(0).Rows.Add(ds.Tables(0).NewRow());
gridView.DataSource = ds;
gridView.DataBind();
int columnCount = gridView.Rows(0).Cells.Count;
gridView.Rows(0).Cells.Clear();
gridView.Rows(0).Cells.Add(new TableCell());
gridView.Rows(0).Cells(0).ColumnSpan = columnCount;
gridView.Rows(0).Cells(0).Text = "No Records Found.";
}
} catch (Exception ex) {
}

posted @ 2006-06-06 08:58 kary 阅读(704) 评论(0) 编辑

2006年5月23日

Web Deployment Projects with Visual Studio 2005

在用VS2005发布网站项目时,存在生成bin目录下的.dll文件名随机生成的问题,现在MS提供了这方面的插件,可以经过简单设置生成自定义的文件名(.dll):

Web Deployment Projects with Visual Studio 2005
下载地址:ttp://download.microsoft.com/download/c/c/b/ccb4877f-55f7-4478-8f16-e41886607a0e/WebDeploymentSetup.msi
MSDN说明:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/web_deployment_projects.asp

简单操作如下:
1.下载后直接安装,安装完后打开VS2005;
2.在[生成]->[发布网站]下面会多出一项[Add Web Deployment Project],也可在要发布的网站项目上点右键.直接选取[Add Web Deployment Project],弹出属性对话框后.设置好指定名称和存放目录添加后,可以看到当前解决方案中多了一项,有很多其它属性,就自己去设置了;
3.在刚添加的方案中点右键->[生成],就会生成相应文件和目录了

posted @ 2006-05-23 09:58 kary 阅读(340) 评论(0) 编辑

2006年5月19日

asp.net2.0如何加密数据库联接字符串

asp.net2.0如何加密数据库联接字符串
在asp.net2.0中,发布网站时,加密web.config,这样可以有效保证数据库用户和密码安全,其步骤如下:

1.添加密钥
执行:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pc "hnlaw" -exp
其中"hnlaw"为密钥名称

2.添加web.config节点
在web.config的<configuration></configuration>之间添加:
<configProtectedData>
  <providers>
    <add keyContainerName="hnlaw" useMachineContainer="true" description="Uses RsaCryptoServiceProvider to encrypt and

decrypt" name="hnlaw" type="System.Configuration.RsaProtectedConfigurationProvider,System.Configuration, Version=2.0.0.0,

Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  </providers>
</configProtectedData>
注意:这里keyContainerName="hnlaw"和name="hnlaw"分别表示你的密钥名称;

3.加密web.config
 到网站根目录添加一个批处理文件enweb.bat,内容如下:
 @echo off
 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pef "system.web/identity" "E:\HS  Studio\Donet2\Hnlawyer" -prov

"hnlaw"
  C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pef "connectionStrings" "E:\HS  Studio\Donet2\Hnlawyer" -prov

"hnlaw"
PAUSE
注册上面的的路径和名称!
运行后出现成功!
4.解密
同样到网站根目录添加一个批处理文件deweb.bat,内容如下:
 @echo off
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pef "system.web/identity" "E:\HS  Studio\Donet2\Hnlawyer"
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pdf "connectionStrings" "E:\HS  Studio\Donet2\Hnlawyer"
PAUSE

最后就是要注意:做完后找到C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys目录下,找到生成

的密钥文件(可按时间查找),给上network service可读取权限,不然会出现Error message from the provider: The RSA key container could

not be opened.
无法读取

这样可能会出现,如果没有自己的服务器,没有权限修改MachineKeys目录,不知道还有其它解决办法,希望大家共享一下:)

posted @ 2006-05-19 12:16 kary 阅读(555) 评论(0) 编辑

2006年5月15日

快速排序与二分查找算法Javascript版本

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> (快速排序与二分查找)</TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="dolphin">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>
<div id="sourceNum"></div>
<div id="sortNum"></div>
<div id="findProcess"></div>
<INPUT TYPE="text" NAME="FindNum" id="FindNum" size="10"><button onclick='javascript: if (/^\d+$/.test(document.getElementById("FindNum").value)){ a.HalfFind(a.sortArray,document.getElementById("FindNum").value)}else{alert("请输入正整数!");document.getElementById("FindNum").value="";document.getElementById("FindNum").focus()}'>Find</button>
<SCRIPT LANGUAGE="JavaScript">
<!--
function Half(sAry)
{
  this.sortArray = sAry;
  this.count = 0;

  this.swap = function(i,j)
  {
    var tmp = this.sortArray[i];this.sortArray[i] = this.sortArray[j];this.sortArray[j] = tmp;
    this.count++;
  }
  this.partition = function(low,high)
  {
    var pivot,pos,tmp,i,j;
    pos = low;
    pivot = this.sortArray[pos];
    for(i=low+1;i<=high;i++)
    {
      if (this.sortArray[i]<pivot)
      {
        pos++;
        this.swap(pos,i);
      }
    }
    this.swap(low,pos);
    return pos;
  }
  this.QuickSort = function(low,high)
  {
    var pivot;
    if (low<high)
    {
      pivot = this.partition(low,high);
      this.QuickSort(low,pivot-1);
      this.QuickSort(pivot+1,high);
    }
  }
  this.HalfFind = function(sArray,fNum)
  {
    var low,high,i,t;
    low = 0;
    i = 1;
    high = sArray.length-1;
    t = Math.floor((high+low) / 2);
    document.getElementById("findProcess").innerHTML="";
    while ((sArray[t]!=fNum) && (sArray[low]!=fNum) && (sArray[high]!=fNum) && (low < high))
    {
      document.getElementById("findProcess").innerHTML+="第"+i+"次查找 -> "+t+"的位置:"+sArray[t]+"&nbsp;&nbsp;&nbsp;&nbsp;low="+low+"&nbsp;high="+high+"<br>";
      
      if (fNum>sArray[t])
      {
        low = t+1;
        t = Math.floor((high+low) / 2);
      }
      else
      {
        high = t-1;
        t = Math.floor((high+low) / 2);
      }
      i++
    }
    if ((sArray[t]!=fNum) && (sArray[low]!=fNum) && (sArray[high]!=fNum) && low >= high)
    {
      document.getElementById("findProcess").innerHTML+="第"+i+"次查找 -> "+t+"的位置:失败 此队列没有"+fNum+"该数&nbsp;&nbsp;&nbsp;&nbsp;low="+low+"&nbsp;high="+high+"<br>";
    }
    else
    {
      if(sArray[t]==fNum)
        tt = t;
      else if(sArray[low]==fNum)
        tt = low;
      else
        tt = high;
      
      document.getElementById("findProcess").innerHTML+="第"+i+"次查找 -> "+tt+"的位置:"+sArray[tt]+" 找到了!&nbsp;&nbsp;&nbsp;&nbsp;low="+low+"&nbsp;high="+high+"<br>";
    }
    return t;
  }
}
var randLen = Math.floor(Math.random()*100);
var stAry = new Array(randLen);
var i,low,high;
for(i=0;i<randLen;i++)
  stAry[i] = Math.floor(Math.random()*100);
var a = new Half(stAry);
low = 0;
high = stAry.length-1;
document.getElementById("sourceNum").innerHTML="排序前:"+a.sortArray+"&nbsp;&nbsp;排序个数:"+(high+1);
a.QuickSort(low,high);
document.getElementById("sortNum").innerHTML="排序后:"+a.sortArray+"&nbsp;&nbsp;排序次数:"+a.count;
//-->
</SCRIPT>
</BODY>
</HTML>

posted @ 2006-05-15 21:24 kary 阅读(477) 评论(0) 编辑

2006年5月11日

随页面移动的浮动广告代码

摘要: 网页中用标准格式时,如:头部<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">在加入浮动广告时,发...阅读全文

posted @ 2006-05-11 09:58 kary 阅读(1160) 评论(0) 编辑

2006年3月27日

MSDN转:自定义实体类简介

摘要: 发布日期: 5/24/2005 | 更新日期: 5/24/2005Karl SeguinMicrosoft Corporation摘要:有些情况下,非类型化的 DataSet 可能并非数据操作的最佳解决方案。本指南的目的就是探讨 DataSet 的一种替代解决方案,即:自定义实体与集合。(本文包含一些指向英文站点的链接。)本页内容 引言 DataSet 存在的问题 自定义实体类 对象关系映射 自定...阅读全文

posted @ 2006-03-27 21:57 kary 阅读(173) 评论(0) 编辑

2006年3月17日

aspnet2.0 Menu控件上传到服务器小问题

摘要: 用ASP.net2.0做好网址上传到到服务器.除后台Menu控件一切正常,速度也还可以.Menu出现了JS错,还有图片无法显示.找了一会儿问题.发现链接:http://localhost:9009/WebResource.axd?d=50yReNXH2VIL5H3VH18sjzp7wPHaYxrvSYByO3u2amI1&t=632764834952031250发现昨天可能删除了.axd的...阅读全文

posted @ 2006-03-17 16:12 kary 阅读(171) 评论(1) 编辑

2006年3月10日

ASP.NET2.0的快速入门站点

摘要: 目前还没有中文版,英文版的地址如下:http://beta.asp.net/QuickStart/default.aspx阅读全文

posted @ 2006-03-10 10:08 kary 阅读(123) 评论(0) 编辑

2006年3月3日

AJAX开发简略

摘要: 七、AJAX开发  到这里,已经可以清楚的知道AJAX是什么,AJAX能做什么,AJAX什么地方不好。如果你觉得AJAX真的能给你的开发工作带来改进的话,那么继续看看怎么使用AJAX吧。7.1、AJAX应用到的技术  AJAX涉及到的7项技术中,个人认为Javascript、XMLHttpRequest、DOM、XML比较有用。A、XMLHttpRequest对象  XMLHttpRequest是...阅读全文

posted @ 2006-03-03 18:00 kary 阅读(91) 评论(0) 编辑