如何生成 .NET MVC 和 Web API 项目并将其发布到其不同的文件夹



我有一个包含MVC和WebApi项目的解决方案。我想做的是一键发布它们。我希望 MVC 项目转到根文件夹..MyApp和 WebApi 以..MyAppApi

我想我已经看到了这样的解决方案,但我只是在任何地方都找不到它。

物理路径将由您的发布方法确定。您可以将发布向导设置为仅指向所需的物理路径。然后,您必须配置 IIS 以确保两个应用程序可以共存。

就路由而言,这将由Web API的内置路由处理。如果将 Web 服务器配置为在 /myapp 处托管应用程序,在 /myapp/api 处托管虚拟目录中的 API,则需要更改 Web API 中的路由以消除api前缀。

config.Routes.MapHttpRoute(
   name: "DefaultApi",
   routeTemplate: "api/{controller}/{id}", //remove "api/"
   defaults: new { id = RouteParameter.Optional }
);

这将告诉 Web API 将其所有路由挂起/而不是/api,因为/api现在是托管应用程序根的位置,并且该路径将来自 IIS。

要进一步回答这些问题,请说明您使用哪种方法进行发布。

可以通过创建宏在一个解决方案中发布多个项目。这方面的一个例子是在MSDN上

Public Module PublishAllProjects
Sub PublishAllProjectsInSolution()
    ' Before using this macro, the certficate and security zone must be set.
    ' You can do this by publishing the projects using the VS IDE.
    Dim slnbld2 As SolutionBuild2 = CType(DTE.Solution.SolutionBuild, SolutionBuild2)
    'Save changes to all projects and clean.
    For Each proj As Project In DTE.Solution.Projects
        proj.Save()
    Next
    slnbld2.Clean(True)
    For Each proj As Project In DTE.Solution.Projects
        'Verify project is a windows application or console application before continuing
        Dim outputType As Integer = proj.Properties.Item("OutputType").Value
        If outputType <> 0 AndAlso outputType <> 1 Then
            Continue For
        End If
        'GenerateManifests and SignManifests must always to true for publishing to work. 
        proj.Properties.Item("GenerateManifests").Value = True
        proj.Properties.Item("SignManifests").Value = True
        proj.Save()
        slnbld2.BuildProject(proj.ConfigurationManager.ActiveConfiguration.ConfigurationName, proj.UniqueName, True)
        'only publish if build was successful.
        If slnbld2.LastBuildInfo <> 0 Then
            MsgBox("Build failed for " & proj.UniqueName)
        Else
            slnbld2.PublishProject(proj.ConfigurationManager.ActiveConfiguration.ConfigurationName, proj.UniqueName, True)
            If slnbld2.LastPublishInfo = 0 Then
                MsgBox("Publish succeeded for " & proj.UniqueName)
            Else
                MsgBox("Publish failed for " & proj.UniqueName)
            End If
        End If
    Next
    End Sub
End Module

最新更新