1

I am working on extension through which I would like to determine current project is on MVC framwork. I am checking this programmatically in .csproj file. Project can on ASP.NET Core Web OR ASP.NET Web Application.

I would like to know specific check/condition which will help me to return true if my application is MVC.

Thanks

cbrebel
  • 85
  • 1
  • 9
  • csproj file is not enough to determine if it is MVC project or not. For .net core 3.0 project they can be exactly the same. And please provide what extension are you working on? (Visual Studio, Visual Studio Code or else..) – Eldar Nov 17 '19 at 12:25
  • You can use both MVC, web API and web pages in the same project. How does that confuse you? – Tanveer Badar Nov 17 '19 at 17:33

2 Answers2

1

Well it's possible to add MVC features to any project, so there's no simple way to tell from the proj file. But generally if it was created as an MVC project in Visual Studio then it will have one of the MVC project type GUIDs.

These are taken from here.

ASP.NET MVC 1   {603C0E0B-DB56-11DC-BE95-000D561079B0}
ASP.NET MVC 2   {F85E285D-A4E0-4152-9332-AB1D724D3325}
ASP.NET MVC 3   {E53F8FEA-EAE0-44A6-8774-FFD645390401}
ASP.NET MVC 4   {E3E379DF-F4C6-4180-9B81-6769533ABE47}
ASP.NET MVC 5   {349C5851-65DF-11DA-9384-00065B846F21}
Model-View-Controller v2 (MVC 2)    {F85E285D-A4E0-4152-9332-AB1D724D3325}
Model-View-Controller v3 (MVC 3)    {E53F8FEA-EAE0-44A6-8774-FFD645390401}
Model-View-Controller v4 (MVC 4)    {E3E379DF-F4C6-4180-9B81-6769533ABE47}
Model-View-Controller v5 (MVC 5)    {349C5851-65DF-11DA-9384-00065B846F21}
Jess
  • 352
  • 1
  • 2
  • 10
0

There are preprocessor defines for Net Core, you can use this to set a constant when the project is running in ASP.NET Core

#if NETSTANDARD1_6
     public const IsCore = true;
#elif NETSTANDARD1_5
     public const IsCore = true;
#elif NETSTANDARD1_4
     public const IsCore = true;
#elif NETSTANDARD1_3
     public const IsCore = true;
#elif NETSTANDARD1_2
     public const IsCore = true;
#elif NETSTANDARD1_1
     public const IsCore = true;
#elif NETSTANDARD1_0
     public const IsCore = true;
#else
     public const IsCore = false;
#endif

Alternatively, if you (can) add the following to your .csproj, you can use a simpler form which won't need to be modified when new frameworks are released:

In .csproj:

<PropertyGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(TargetFramework)', '^netcoreapp\d'))">
  <DefineConstants>NETCORE</DefineConstants>
</PropertyGroup>

And the following code somewhere to set up the constant:

#if NETCORE
     public const IsCore = true;
#else
     public const IsCore = false;
#endif

See How to set .NET Core in #if statement for compilation

Derrick
  • 2,502
  • 2
  • 24
  • 34