3

I am using Sitecore 8.2 rev 1 with .Net framework 4.6.1.

I am trying to use a null-conditional operator in one of my views. After I publish my site, it gives a compilation error on the line of code using this operator. I read online that Sitecore 8.2 works with .Net 4.6.1.

What do I need to do to get this operator to work?

  • What version of MVC are you using? – Nathan Hase Feb 22 '17 at 15:22
  • I am using MVC 5.2.3 – Iceape Feb 22 '17 at 15:23
3

The best way to to this is to include any logic in the Model for the View, avoid putting code directly in the View when possible.

However: In MVC5 and below you'll need to enable the use of new .net features within the Views.

Add this Nuget package to your solution if you are using MVC5.

https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/

The nuget package should modify your web.config, but check that the following configuration is in your web.config file (and if it isn't add it in):

<system.codedom>
<compilers>
  <compiler language="c#;cs;csharp" extension=".cs"
    type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
  <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
    type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
</compilers>

More information: https://stackoverflow.com/questions/30832659/string-interpolation-in-a-razor-view https://blogs.msdn.microsoft.com/webdev/2015/12/07/new-feature-to-enable-c-6-vb-14/

| improve this answer | |
  • This package is already installed in my project and my web.config (local to this project) contains this section. However, Sitecore's web.config located in the web root did not have this section. I added it and am now getting the error "Could not load file or assembly 'Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)" – Iceape Feb 22 '17 at 15:47
  • Also, I can see this package in the bin folder of my web root. Is there another setting I need to change in order for Sitecore to locate this? – Iceape Feb 22 '17 at 15:57
  • I updated the answer - the best way to do this is to use the Model. Create additional properties for computed values. This issue you're seeing can probably be fixed by updating the assembly bindings in the application web.config (not the View web.config. – Nathan Hase Feb 22 '17 at 15:59
  • Okay, thank you. I will try moving it to the model – Iceape Feb 22 '17 at 16:09
2

Go to your project properties in Visual Studio. Click on Build -> Advanced. Set C# 6.0 as default language.

Also, this stack overflow question suggests multiple solutions:

https://stackoverflow.com/questions/27968963/c-sharp-6-0-features-not-working-with-visual-studio-2015

| improve this answer | |
  • Thank you for the suggestion. I have already tried all of the solutions mentioned on this page. My guess is that there is something in the Sitecore web.config file that is not correct. After adding the <system.codedom> section to the Sitecore web.config, I get a new error stating that it cannot locate the assembly. I am hoping that once I figure out why it cannot be located (despite the assembly being in my bin folder along with all the other assemblies) that I can fix it and get it to work. – Iceape Feb 22 '17 at 22:15
1

I think the default web.config from Sitecore still has <compilation defaultLanguage="c#" debug="false" targetFramework="4.5.2">. So you might need to change the targetFramework there.

| improve this answer | |
  • Good find. I updated that section to 4.6.1. I also found a tag called <HttpRuntime> that was set to 4.5.2 so I updated it to 4.6.1 as well. However, I am still receiving the same error – Iceape Feb 22 '17 at 15:48
1

You need to be sure that in your /Views folder in your website, you have a web.config file there that has something like this in it: https://gist.github.com/sitecoreignition/62a81aea68ca14c4ed7ee87180b78cb9

Also, you should be sure you have the following code in your main site web.config (it should be there if you have installed the Roslyn stuff). https://gist.github.com/sitecoreignition/60637b1a87a599023d42f41e73bb4c42

I don't deploy my web.configs typically so I had to make sure those got copied over to my site. You might still get some intellisense or syntax highlighting in your views inside Visual Studio, but your code will run just fine on the server.

If everything else is configured correctly, it should all work.If you download the Ignition project template you'll get the Views folder version of the web.config automatically. That can be found here: https://github.com/sitecoreignition/Ignition.Foundation/blob/master/Ignition.Template.Project.zip

| improve this answer | |
  • Thank you for the help. I have that code in my main site web.config already, but my views folder does not have the <assemblyBinding> section. I will add that and experiment with it later. – Iceape Feb 28 '17 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.