如何使用MSBuild编译ASP.Net MVC项目

dc.*_*dc. 14 asp.net msbuild asp.net-mvc

如何使用MSBuild编译ASP.Net MVC项目?我们使用Continuous Integration服务器来编译和部署我们的应用程序.为了简单起见,我在VS2008中创建了一个MVC 1.0项目.我立即创建了一个MSBuild脚本文件来编译它.我没有更改项目中的任何代码.MSBuild脚本包含以下目标.

   <AspNetCompiler
      VirtualPath="/"
       PhysicalPath="C:\Development\mvc1\"
        TargetPath="c:\publish\xxx"
        Force="true"
        Debug="false" 
 Updateable="true"
Run Code Online (Sandbox Code Playgroud)

MVC项目sln文件包含在c:\ development\mvc1 \目录中.我正在运行XP/Pro.

我收到错误ASPCONFIG:使用注册为allowDefintion ='MachineToApplication'的部分超出应用程序级别是错误的.我从Web配置文件中删除了身份验证模式,成员资格提供程序等,直到我最终看到不同的错误信息.我现在收到一条错误消息,指出文件'/views/shared/site.master'不存在.

到底是怎么回事?在此先感谢您的帮助!

我使用错误的MSBuild命令吗?

Tuo*_*nen 6

如果你编译你的sln文件(msbuild mysolution.sln)或

<MSBuild Projects="msbuild mysolution.sln" Targets="Rebuild" ContinueOnError="false"
StopOnFirstFailure="false" /><!-- -d -errorstack -->
Run Code Online (Sandbox Code Playgroud)

并且sln文件具有ASP.NET MVC项目.csproj文件,然后.csproj文件确实包含您需要的所有内容.用记事本打开.csproj并查找:

1)这应该是真的:

<MvcBuildViews>false</MvcBuildViews>
Run Code Online (Sandbox Code Playgroud)

2)目标名称="AfterBuildCompiler":

  <Target Name="AfterBuildCompiler" Condition="'$(MvcBuildViews)'=='true'">
    <AspNetCompiler VirtualPath="SomeVirtualDir" PhysicalPath="C:\Development\mvc1\" TargetPath="c:\publish\xxx\" />
  </Target>
Run Code Online (Sandbox Code Playgroud)

我没有做任何其他事情而且有效.我实际上创建了我的配置,以便只有发布版本部署应用程序(通过移动PropertyGroups下的MvcBuildViews属性.然后我可以在开发(调试)和部署(发布)中使用相同的.csproj.


tgg*_*ggm 6

此构建脚本编译asp.net MVC 3应用程序.由于整个互联网似乎已经忘记了"构建脚本"的概念,因此不要求你在目标机器上安装Visual Studio或"lol,你只需要编辑你的csproj文件来获得msbuild !!"

继续.

确保安装了.NET 4和MVC3.顺便说一句,我的构建脚本只适用于msbuild 4,因此请确保使用正确的构建脚本.

一般过程如下(感谢我得到的许多提示和答案!)

1) Build the dependencies (you DLL's)
2) Build the DLL for your web application.
3) Call the asp.net compiler task.
4) Check the scripts for additional comments.
Run Code Online (Sandbox Code Playgroud)

请注意,这是从编译其他DLL的外部脚本调用的(业务,数据访问等)

<Project ToolsVersion="4.0" DefaultTargets="build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
        <PropertyGroup>
        <BuildDir>..\..\dist</BuildDir>
        <Optimize>true</Optimize>
    </PropertyGroup>
    <ItemGroup >
        <Reference Include="System.dll" />
        <Reference Include="System.Core.dll" />
        <Reference Include="System.Web.Abstractions.dll" />

<!-- add the remaining DLL's required. Check your References folder inside VS2010 and add the relevant entries here. It's a lot of references. I ommited them to make the post more compact.
For reasons that are beyond me, I only managed to get some DLL's referenced by full path. Go figure... -->

        <Reference Include="C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.Helpers\v4.0_1.0.0.0__31bf3856ad364e35\System.Web.Helpers.dll" />
        <Reference Include="C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.Mvc\v4.0_3.0.0.0__31bf3856ad364e35\System.Web.Mvc.dll" />
        <Reference Include="C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages\v4.0_1.0.0.0__31bf3856ad364e35\System.Web.WebPages.dll" />

<!-- The "main build script" compiles the other DLL's from the project and places them on the BuildDir folder. Just reference it here-->
        <Reference Include="$(BuildDir)\*.dll"></Reference>
    </ItemGroup>

<!-- Build a DLL for the code file inside your web project (controllers, models, the lot...) place it together with the other DLL's 
WARNING: Simple build command. Resource files are not included in this.
-->

    <Target Name="BuildWebDll">
        <ItemGroup>
            <CodeFiles Include=".\**\*.cs" />
        </ItemGroup>
        <CSC Sources="@(CodeFiles)" TargetType="Library" References="@(Reference)" OutputAssembly="$(BuildDir)\cth.web.dll" >
        </CSC>      
    </Target>

<!-- For reasons also unkown, but covered in a number os posts in this forum, the asp.net compiler requires the necessary DLL's to be placed on the BIN/ folder of your web project. That's why we're copying every DLL we need to said folder. For debugging, check the Bin folder on Visual Studio after you compile the project. You need to replicate that in your BIN/
-->
    <Target Name="CopyDLLs">
        <ItemGroup>
            <DllFiles Include="$(BuildDir)/*.dll"/>
        </ItemGroup>
        <Copy SourceFiles="@(DllFiles)" DestinationFolder="Bin\"></Copy>
    </Target>

    <Target Name="build">
        <CallTarget Targets="BuildWebDll"></CallTarget>
        <CallTarget Targets="CopyDLLs"></CallTarget>

        <!-- Call this from the webproject directory. PhysicalPath references ".". TargetPath can be everything you want -->
        <AspNetCompiler Updateable="true" VirtualPath="/CTH.Web" PhysicalPath="./"  TargetPath="$(BuildDir)/CTH.Web" Force="true" Debug="false"    />
    </Target>
Run Code Online (Sandbox Code Playgroud)

请记住,您必须包含资源文件,执行任何web.config替换等.我真的希望这会有所帮助.