How to build and publish .NET MVC and Web API proj

2019-07-04 01:20发布

问题:

I have a solution which contains MVC and WebApi projects. What I want to do is to to publish them both with a single click. I would like MVC project to go to a root folder ..\MyApp\ and WebApi to ..\MyApp\Api\.

I think I've seen such solution, but I just cannot find it any where.

回答1:

The physical path is going to be determined by your publishing method. You could set up the publishing wizard to just point to the physical path that you want. Then you'd have to configure IIS to ensure that both applications can coexist.

As far the routing, that will be taken care of by Web API's built in routing. If you configure your web server to host the application at /myapp and the API in a virtual directory at /myapp/api, you'll need to change the routing in Web API to eliminate the api prefix.

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

This will tell Web API to hang all of it's routes off of / instead of /api since /api is now where the root of the application is hosted, and that path will come from IIS.

To further answer the questions, please specify what method you are using to publish.



回答2:

It is possible to publish multiple projects in one solution by creating a macro. An example of this is on 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