Use link files in ASP. NET web applications

Source: Internet
Author: User
Recently, I reconstructed an internal platform system. As a platform, there are several subsystems under it, each of which has its own website system. Each website uses a uniform style, a unified verification mechanism, and a feedback system. Therefore, in order to avoid repeated occurrences of the same resource or file in several subsystems, I plan. net web site to ASP. net web application, and then solve this problem by linking external public files. At the same time:

1. Web application is a Web site upgrade product.
2. Web application allows you to add links by adding files from other directories to the project as links, making it more flexible.
3. Web application compilation, construction, and deployment are simpler, faster, and more convenient.

Of course, there are many differences between web application and web site, such:

1. The web application has the designer. CS file, and the Web site does not.
2. The web application has a namespace, and the Web site does not exist by default.
3. The web application does not have the app_code directory by default. You must manually add the directory, and the default attribute of the added CS file is content. You must manually change it to compile to add the file for compilation.
...

And so on. This article mainly describes some problems and solutions encountered when using linked files in ASP. NET web applications.

First, we will introduce 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. There are several steps:
1. Create a web application project and add the original web site page to the project.
2. Add a namespace to the class in the Aspx. CS file. In addition, the inherits attribute of the aspx file header is added with a namespace.
3. Right-click the aspx file or project name and click "convert to Web application ". The designer. CS file is automatically generated. (Definition of controls on the ASPX page .)

Now, add an external link file:

Adding a linked file is simple and convenient. However, debugging may cause a lot of trouble. During debugging, the built-in vs web server is used by default. The root directory of the website isSource codeDirectory. During debugging, an error occurs because the file cannot be found.

If you use the "publish" menu to publish a website, the linked resource file will be copied. This is what we want. Publishing is required for each debugging, and setting the virtual directory in the local IIS is too troublesome.

At the same time, we hope to automatically build and release the website through msbuild, and we also hope to automatically copy the linked files. The command for compiling the ASP. NET web application project in msbuild is:

< Msbuild Projects = "D: \ demo \ WEB. csproj" Targets =" Resolvereferences; _ copywebapplication ;" Properties = "Webprojectoutputdir = D: \ publish \; outdir = D: \ publish \ bin \; configuration = release" > </ Msbuild >

However, the above command will not copy the linked file. This problem has plagued me. Do I need to write a copy task and copy the required files by myself? Later, Google found that some people encountered the same problem as me, and provided an excellent solution and solved the debugging and release problems. It was perfect!

The method is to modify the csproj file, redefine the default _ copywebapplication target, and increase the target of the copy link file. Set the following sectionCodeAdd to the csproj file:

<! --
========================================================== ================================
_ Copywebapplication
Modified: ignores linked files as part of normal deployment logic.
This target will copy the build outputs along with
Content files into a _ publishedwebsites folder.
This task is only necessary when $ (outdir) has been redirected
To a folder other ~ \ Bin such as is 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 >
<! --
========================================================== ================================
Copy‑contentfiles
A new target to copy any linked content files into
Web application Output Folder.
Note: This is necessary even when '$ (outdir) 'has not been redirected.
========================================================== ================================
-->
< Target Name = "Copymediacontentfiles" >
<! -- 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 -->
<! -- Include the new _ copy1_contentfiles target. -->
< Propertygroup >
< Prepareforrundependson >
$ (Prepareforrundependson );
_ Copywebapplication;
Copy‑contentfiles;
_ Builtweboutputgroupoutput
</ Prepareforrundependson >
</ Propertygroup >
</ Project >

In fact, some general copies are written, without the need to manually specify which copies are required. Then, in the msbuild script, add copy‑contentfiles target:

< Msbuild Projects = "D: \ demo \ WEB. csproj" Targets =" Resolvereferences; _ copywebapplication; copymediacontentfiles" Properties = "Webprojectoutputdir = D: \ publish \; outdir = D: \ publish \ bin \; configuration = release" > </ Msbuild >

Done! At this time, the files compiled by msbuild will include all the files we need. At the same time, click "build" in vs to compile and copy the linked files to the source code directory. This makes it very convenient to use the built-in web server for debugging!

Reference link:
Http://consultingblogs.emc.com/jamesdawson/archive/2008/06/03/using-linked-files-with-web-application-projects.aspx

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.