2

I'm using Visual Studio 2017 with an MVC project targeting the .Net Core 2.0 framework. In a view I'm getting the interpolated strings error along with one for async function.

When I first received it, I changed in Properties -> Build -> Advanced the Language Version from 4.0 to 7.1 compiled with no luck. I also found a couple of other solutions saying to install nuget packages Microsoft.Net.Compilers and another Microsoft.CodeDom.Providers.DotNetCompilerPlatform. After installing both packages, I still have the error in my view.

I did a quick test of the syntax in my controller and it seems to be fine and doesn't throw an error. I thought when you install a new version of VS, you get the most recent version of C#? Do I need to install the C# compiler or something?

This is the code that is causing the problem:

<p class="text-center">
    @if (item is Jamboree)
    {
        ((Jamboree)item).EventDate.Date.ToString();
    }
    else
    {
        $"{((Shootout)item).EventBegDate.Date} thru {((Shootout)item).EventEndDate.Date.ToString()}";
    }
</p>
Servy
  • 202,030
  • 26
  • 332
  • 449
cnotes
  • 247
  • 1
  • 4
  • 15
  • Try restarting VS after installing the CodeDom packages. –  Nov 09 '17 at 17:00
  • @Amy - Thanks for the suggestion Amy. Just tried it and it didn't work. – cnotes Nov 09 '17 at 17:24
  • Do you have issues with C#6 code in your controller? It might be specific to your Razor configuration https://stackoverflow.com/questions/31689374/how-to-make-razor-view-engine-to-use-c-sharp-6-0 – Kevin Secrist Nov 09 '17 at 18:01
  • @KevinSecrist - No, I don't seem to have any issues with C#6 code in the controller. I tested string interpolation in the controller and it did not error. What could be wrong with my Razor configuration? I'm having other issues in my view like it doesn't recognize the 'Model', 'ViewData', or sometimes the '@model' or like now the actual model the view is typed as. This is not a problem in my other views. In the 1st view I created, it had the red squiggly lines but went away on its own, but this view it doesn't. Thanks for any help. – cnotes Nov 09 '17 at 18:09
  • @cnotes you have a web.config inside your views folder. Please paste it into the question. –  Nov 09 '17 at 18:41
  • @Amy - I don't see the web.config inside of my views folder....even clicked the view all files button and didn't see one. Am I missing something? – cnotes Nov 09 '17 at 18:50
  • Yes, there is supposed to be a `web.config` inside your views folder. –  Nov 09 '17 at 21:33
  • @Amy - I not so sure about that with .Net core 2.0. For views, they have a ViewImports.cshtml file as well as the appsettings.json file in place of the web.config file. The web.config may still be usable, but I have the ViewImports.cshtml and json file in my project. I'm not using web.config anywhere. – cnotes Nov 09 '17 at 21:40

2 Answers2

2

To address the two problems you are having:

1) Use of async/await in your View

Using async/await isn't possible in a view. You should try and do this in your controller instead. See Use of await in Razor views

2) Use of interpolated strings

I think this is really just because of a syntax error in your question. You need to surround your strings with @() in order to print them. If it's a value that is potentially unsafe (like data coming from your database/users) it should be surrounded with Html.Encode() instead.

<p class="text-center">
    @if (item is Jamboree)
    {
        @(((Jamboree)item).EventDate.Date.ToString())
    }
    else
    {
        @($"{((Shootout)item).EventBegDate.Date} thru {((Shootout)item).EventEndDate.Date.ToString()}")
    }
</p>
Kevin Secrist
  • 821
  • 10
  • 23
  • There's a yellow triangle next to the Microsoft.CodeDom.Providers.DotNetComplierPlatform entry in my dependencies list stating that it was restored using the .net 4.6.1 version and not that of my target framework .netcoreapp version 2.0. Is there another version that I need? Or, how do I proceed from here? – cnotes Nov 09 '17 at 18:53
  • That might be just be a warning, .NET Framework 4.6.1 is compatible with .NET Core 2.0 and there is no version of that package which is directly targeted to .NET Standard 2.0. Did you add this to your web.config? It would be in the root of your project. – Kevin Secrist Nov 09 '17 at 19:10
  • There is no web.config in my project. .Net Core 2.0 uses the csproj file for configuration. Do I need to add a web.config file to the root of my project then add the code you suggested? – cnotes Nov 09 '17 at 19:27
  • @cnotes I think the codedom stuff may not be relevant, I have rewritten my answer. I created a brand new project using `dotnet new razor` and was able to use interpolated strings just fine. However, make sure you have VS 15.3 and the .NET Core SDK 2.0 installed, just to make sure. – Kevin Secrist Nov 09 '17 at 20:08
  • Thanks for your help. I do have VS 15.3 & am going to upgrade to 15.4 & SDK 2.0 is installed. I've got to figure out why the ViewData and Model keep saying doesn't exist in current context in my view....that's my main issue. If you have any ideas on that one, please https://stackoverflow.com/questions/47162171/viewdata-model-does-not-exist-in-the-current-context. Thanks again!!! – cnotes Nov 09 '17 at 20:31
0

Since I was receiving so many errors with this view....errors I wasn't even trying to do within the view, I ended up creating another project and copying everything over to the new project. Now everything works like a charm. I think something got out of whack with the project....not sure what.

cnotes
  • 247
  • 1
  • 4
  • 15