Using linked files in ASP. NET WEB Application

Source: Internet
Author: User

Recently refactoring an internal platform system, as a platform, under which there are several subsystems, each subsystem has its own website system. Each website uses a unified style, a unified authentication mechanism, a feedback system, and so on. So, to avoid repeating the same resources or files in several subsystems, I intend to convert all of the previous ASP. NET Web site to ASP. NET Web application, and then resolve this problem by linking external public files. While

1. Web application is an upgrade product for Web site.
2. Web application allows you to add links to other directory files as a link to the project, more flexibility.
3. Web application compile, build, deploy more simple, fast and convenient.

Of course, Web application and Web site have many different places, such as:

1. Web application has designer.cs file, Web site does not.
2. The Web application has a namespace and the Web site does not have a default.
3. The WEB application default does not have the App_Code directory, need to add manually, and the added CS file default property is content, need to manually modified to compile to join the compilation.
...

Wait a minute. This article focuses on some of the problems and workarounds that are encountered when using linked files in ASP. NET WEB application.

First, describe how to convert a Web site page to a Web application page. If you know the difference between the two, it will be very easy. Mainly divided into several steps:
1. Create a new Web application project to add the original Web site page to the project.
2. In the Aspx.cs file, add a namespace to the class. Also, the Inherits property of the header of the ASPX file is prefixed with the namespace.
3. Right-click the aspx file or project name and tap Convert to Web application. At this point, the Designer.cs file is automatically generated. (The definition of a control in an ASPX page.) )



OK, add the external link file:





Adding a linked file is simple and convenient. However, during the debugging process, you will encounter a lot of trouble. Because when debugging, the default use of the VS built-in Web Server, the root directory of the Web site is the source code directory, debugging, when you encounter a linked file, you will not find the file and error.

If you use the "Publish" menu to publish a Web site, the linked resource files will be copied over, which is what we want. Every time you debug, you need to publish, and then set up the virtual directory in the native IIS, which is too much trouble.

At the same time, we want to automatically build and publish the site through MSBuild, and build it with the desire to automatically copy the linked files to the past. The commands in MSBuild to compile the ASP. NET WEB Application project are:

<msbuild projects= "d:\Demo\Web.csproj" targets= "resolvereferences;_copywebapplication;" Properties= "webprojectoutputdir=d:\publish\;outdir=d:\publish\bin\; Configuration=release "></MSBuild>


However, the above command does not copy the linked file. This question bothers me, do I have to write a copy of the task, I will need to copy the files past? Google a bit later, found that some people meet me the same problem, and provide a great solution, while solving the debugging and publishing problems, it is perfect!

The method is to modify the csproj file, redefine the default _copywebapplication target, and increase the target of the copy link file. Add the following code to the csproj file:

<!--
============================================================
_copywebapplication
Modified:ignores linked files as part of normal deployment logic.
This target would copy the build outputs along with the
Content files into a _publishedwebsites folder.
This Task was only necessary when $ (OutDir) had been redirected
to a folder and other than ~\bin such as are the case with Team Build.
============================================================
-
<target name= "_copywebapplication" condition= "' $ (OutDir) '! = ' $ (OutputPath) '" >
<!--Log Tasks----
<message text= "Copying Web application Project Files for $ (msbuildprojectname)"/>
<!--Create the _publishedwebsites\app\bin folder---
<makedir directories= "$ (webprojectoutputdir) \ Bin"/>
<!--Copy build outputs to _publishedwebsites\app\bin folder---
<copy sourcefiles= "@ (intermediateassembly)" destinationfolder= "$ (webprojectoutputdir) \ Bin" SkipUnchangedFiles= "True"/>
<copy sourcefiles= "@ (addmodules)" destinationfolder= "$ (webprojectoutputdir) \ Bin" skipunchangedfiles= "true"/ >
<copy sourcefiles= "$ (intermediateoutputpath) $ (_sgendllname)" destinationfolder= "$ (webprojectoutputdir) \% ( Content.subfolder)% (content.recursivedir) "skipunchangedfiles=" true "condition=" ' $ (_sgendllcreated) ' = = ' true ' "/ >
<copy sourcefiles= "$ (intermediateoutputpath) $ (TargetName). pdb" destinationfolder= "$ (webprojectoutputdir) \ Bin" Skipunchangedfiles= "true" condition= "' $ (_debugsymbolsproduced) ' = = ' true '"/>
<copy sourcefiles= "@ (docfileitem)" destinationfolder= "$ (webprojectoutputdir) \ Bin" skipunchangedfiles= "true" Condition= "' $ (_documentationfileproduced) ' = = ' true '"/>
<copy sourcefiles= "@ (intermediatesatelliteassemblieswithtargetpath)" destinationfiles= "@ ( Intermediatesatelliteassemblieswithtargetpath-> ' $ (webprojectoutputdir) \bin\% (Culture) \$ (TargetName). Resources.dll ') "skipunchangedfiles=" true "/>
<copy sourcefiles= "@ (referencecomwrapperstocopylocal); @ (resolvedisolatedcommodules); @ (_deploymentloosemanifestfile); @ (nativereferencefile) "destinationfolder=" $ (webprojectoutputdir) \ Bin "skipunchangedfiles=" true "/>
<!--copy any referenced assemblies to _publishedwebsites\app\bin folder--
<copy sourcefiles= "@ (referencecopylocalpaths)" destinationfolder= "$ (webprojectoutputdir) \ Bin" Skipunchangedfiles= "true"/>
<!--modification Here:copy local content files (i.e. non-linked files) recursively to _publishedwebsites\app\ folder -
<copy condition= "'% (content.link) ' = = '" sourcefiles= "% (content.identity)" Destinationfolder= "$ ( Webprojectoutputdir) \% (content.relativedir) "/>
</Target>
<!--
============================================================
Copylinkedcontentfiles
A new target to copy any linked content files into the
Web application output folder.
Note:this was necessary even when ' $ (OutDir) ' had not been redirected.
============================================================
-
<target name= "Copylinkedcontentfiles" >
<!--Remove Any old copies of the files----
<delete condition= "'% (content.link) '! = ' and Exists (' $ (webprojectoutputdir) \% (content.link) ')" Files= "$ ( Webprojectoutputdir) \% (content.link) "/>
<!--Copy linked content files recursively to the project folder---
<copy condition= "'% (content.link) '! = '" sourcefiles= "% (content.identity)" destinationfiles= "$ ( Webprojectoutputdir) \% (content.link) "/>
</Target>
<!--Override The default target dependencies to--
<!--include the new _copylinkedcontentfiles target. -
<PropertyGroup>
<PrepareForRunDependsOn>
$ (Prepareforrundependson);
_copywebapplication;
Copylinkedcontentfiles;
_builtweboutputgroupoutput
</PrepareForRunDependsOn>
</PropertyGroup>
</Project>


The fact is that you write some generic copy, without having to manually specify what needs to be copied. Then, in the MSBuild script, add copylinkedcontentfiles Target:

<msbuild projects= "D:\Demo\Web.csproj" targets= " resolvereferences;_copywebapplication; Copylinkedcontentfiles "Properties= "webprojectoutputdir=d:\publish\;outdir=d:\publish\bin\; Configuration=release "></MSBuild>


Get! The files that MSBuild compiles will then include all the files we need. At the same time, clicking "Build" in VS will also copy the linked file to the source code directory, which makes it easy to debug with the built-in Web server!

Using linked files in ASP. NET WEB Application

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.