11

Previous versions of ASP.NET allowed you to auto-increment the version number via Project Properties. How can I do this in MVC 6?

Stafford Williams
  • 9,696
  • 8
  • 50
  • 101
  • Possible duplicate of [Auto Versioning in Visual Studio 2017 (.NET Core)](https://stackoverflow.com/questions/43019832/auto-versioning-in-visual-studio-2017-net-core) – Michael Freidgeim Oct 28 '17 at 03:55

3 Answers3

12

MVC 6 now uses project.json to track version and you can bump this number using gulp-bump.

Version Bumping

  1. Add gulp-bump to package.json > devDependencies

    gulp-bump": "1.0.0"

  2. Edit gulpfile.js

    • Add bump = require("gulp-bump") to the dependencies at the top
    • Add a task to bump the version number

      gulp.task("bump", function() {
        gulp.src("./project.json")
        .pipe(bump())
        .pipe(gulp.dest("./"));
      });
      
  3. Update project.json

    • By default the MVC template sets the version number to 1.0.0-*, change this to 1.0.0.
    • Add "gulp bump" to the bottom of "scripts" > "prepublish"

Now whenever you Publish, or dnu publish or run the gulp Task Runner the version number will bump.

Bonus

To display this version number in View add the following in the view;

@inject Microsoft.Extensions.PlatformAbstractions.IApplicationEnvironment appEnv
My version number is @(appEnv.ApplicationVersion)
Stafford Williams
  • 9,696
  • 8
  • 50
  • 101
  • 2
    This is pretty cool but the problem with this approach is that this will not work if you are using source control. The version number will not be saved back to it. See my answer for a solution to those who are using source control and build machines. – Muhammad Rehan Saeed Nov 20 '15 at 15:32
  • 1
    Your comment doesn't make sense. This solution bumps the version number inside project.json - allowing that same project.json to then be committed to source control... I think you mean build server rather than source control... – Stafford Williams Nov 20 '15 at 23:05
  • 1
    yes, sorry. Perhaps I was not clear, I mean that if using a build server, you would need to have a step to check-out and check-in the project.json file. – Muhammad Rehan Saeed Nov 21 '15 at 11:51
  • How can we do this for .net core class library ? I want to publish that as nuget package but with version updated automatically whenever i run `dotnet pack`. – Venkata Dorisala Sep 30 '16 at 15:53
4

ASP.NET 5 (DNX) Answer

This is what the ASP.NET 5 team actually uses themselves. If you are using a continuous integration build server you can get your build server to set the DNX_BUILD_VERSION environment variable like so using PowerShell:

$env:DNX_BUILD_VERSION=$version

Your build machine then sets $version to 'build123' or something similar (It can't start with a number, has to be a character from the alphabet) Then, as long as your version number is set like so:

{
    "version": "1.0.0-*"
}

The star will be replaced with the value in the DNX_BUILD_VERSION environment variable. See the ASP.NET 5 GitHub page here for more info.

Community
  • 1
  • 1
Muhammad Rehan Saeed
  • 35,627
  • 39
  • 202
  • 311
  • What about if you have many projects that should share the same version? Before it was easy having a GlobalAssemblyInfo added as link and changing the version in one place. – Daniel Jul 12 '16 at 19:25
  • @Daniel The above would only sync the build version. You could write a little PowerShell to read the project.json JSON and then set the version number. – Muhammad Rehan Saeed Jul 13 '16 at 07:28
  • I find this answer useless, it provides zero guidance. Could you please update it with scripts to be created with their location, and how to integrate them into an existing solution? Thx – Max May 20 '17 at 18:11
1

For .NET Core (RTM) projects, you can use dotnet-bump. You can add it as a tool to your project, and call it from a postcompile script. http://github.com/BalassaMarton/dotnet-bump

Márton Balassa
  • 818
  • 7
  • 11