如何在msbuild配置文件(* .proj)中使用绝对路径?

时间:2018-07-15 05:45:36

标签: msbuild appcmd

msbuild.proj文件中的相对内容:

<Message Text="*****check the site exists*****" Importance="high"/>
<Exec Command="C:\WINDOWS\System32\inetsrv\appcmd.exe list site /name:$(WebAppSiteName) " ContinueOnError="true">
  <Output TaskParameter="ExitCode" PropertyName="ErrorCode2" />
</Exec>

<Message Text="*****if not exists create site*****" Importance="high"  Condition="'$(ErrorCode2)' > '0'" />
<Exec Command="C:\WINDOWS\System32\inetsrv\appcmd.exe add site /name:$(WebAppSiteName) /bindings:http/*:80:$(SiteDomain) /applicationDefaults.applicationPool:$(WebAppSiteName) /physicalPath:$(BuildSolutionDir)$(DeployDir)Website" Condition="$(WebAppSiteName)!=''  and '$(ErrorCode2)' > '0'"></Exec>
上面的

:运行时的实际参数(/ physicalPath)值为: D:\ YDJWebsite.Dev \ deply \ fw \ .. \ .. \ mkltest2Website

实际上,以上路径等效于 D:\ YDJWebsite.Dev \ mkltest2Website

访问网站时,错误显示: 500内部服务器错误, 找不到资源

如果我将路径更改为正确的格式“ D:\ YDJWebsite.Dev \ mkltest2Website ”,则错误消失了

the physical path of website in iis shows here.

现在,我知道唯一的解决方案是将参数值(/ physicalPath)转换为正确的格式?

但是如何在proj文件中执行此操作?有什么建议吗? tks。

1 个答案:

答案 0 :(得分:0)

我已经解决了msbuild的内联任务的问题。

<UsingTask TaskName="AbsolutePath" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
  <ParameterGroup>
      <Path ParameterType="System.String" Required="true" />
      <FullPath ParameterType="System.String" Output="true" />  
  </ParameterGroup>
  <Task>
      <Reference Include="System.Core" />
      <Using Namespace="System" />
      <Using Namespace="System.IO" />
      <Using Namespace="System.Text.RegularExpressions" />
      <Using Namespace="Microsoft.Build.Framework" />
      <Using Namespace="Microsoft.Build.Utilities" />
      <Code Type="Fragment" Language="cs">
          <![CDATA[
          try {
                if(string.IsNullOrWhiteSpace(Path))
                {
                    FullPath = "";
                }
                else
                {
                    DirectoryInfo dirInfo = new DirectoryInfo(Path);
                    FullPath = dirInfo.FullName;
                }
          }
          catch (Exception ex) {
            FullPath = "";
          }
      ]]>
      </Code>
  </Task>

<AbsolutePath Path="$(BuildSolutionDir)$(DeployDir)Website">
        <Output TaskParameter="FullPath" PropertyName="FullPath" />
    </AbsolutePath>
<Message Text="$(FullPath)"/>

tks。