Why does visual studio think js file is a cs file? - asp.net

I have a ASP.NET solution in Visual Studio 2008 and I added a file identical to http://plugins.jquery.com/files/jquery.cookie.js.txt named jquery.cookie.js in a subfolder of my project containing other javascript files and Visual Studio is treating it as a C# file, giving me errors like CS1012: Too many characters in character literal and Semicolon after method or accesssor block is not valid.
Why?

Open the .js file properties in Visual Studio and make sure that Build Action is Content or None, but not Compile.

Ok, figured it out. I accidentally had runat="server" on one of the <script> tags that included the file. I guess that caused it to try to compile

Related

How to set the language a plain text is read as in Visual Studio

I've been using Notepad++ for editing CSS, and for just plain text. In Notepad++ I can paste a block of text and specify if I want the file to be highlighted as HTML, CSS, etc.
How do I set the language that Visual Studio will use to interpret a block of text I pasted from my clipboard? What about when I open a plain TXT file?
Am I using the wrong tool for the job? I've been hearing about "Visual Studio Code". Is that what I should use instead?
In general, Visual Studio works with "Project". It is designed to work with the relevant files for the project, and it will recognize them according to the extension.
In Visual Studio, you can override the default config for the file extension with this docs.microsoft. This configuration will work against all files with this extension, but not against a specific file.
I guess there are plugins that can help on the subject, but as you said, Visual Studio Code is more like a file editor like Notepad++, with very powerful plugins and capabilities. So yes, in case you want an enhanced Notepad++, you need to use the Visual Studio Code and not the Visual Studio.
After using both Visual Studio and Visual Studio Code I can confidently tell you VS Code is so much swifter to open and use while also being very easy to navigate.
VS Code automatically detects the language of your code but you can also switch languages by clicking on the existing language at the bottom.
I highly recommend you use it: https://code.visualstudio.com/

Razor Syntax Highlighting

I have a fairly large ASP MVC project which I am running in visual studio 15.4.5. Whenever I view .cshtml files Visual Studio incorrectly highlights the file for example:
We are making use of compiled razor views.
The project compiles and runs correctly
What I've tried
Deleting all bin, obj & .vs folders
Running .\devenv.exe /ResetUserData
Running .\devenv.exe /UpdateConfiguration & .\devenv.exe /ClearCache
I found this question whilst trying to solve the nuisance that is the highlighting myself so thought I'd share my findings. Apparently this is the intentional behaviour as it referred to as "Razor Syntax Highlighting" and through both this post on MSDN and another question on here: How do you turn off Razor highlighting in VS 2013? I was able to determine how to turn it off on my machine.
Quote from MSDN post:
This highlighting is the built in support for Razor syntax highlighting. If you do not like it, you can open your .cshtml files in Visual Studio using the HTML editor
Screenshot of what I changed that worked for me:

Project template for Visual Studio 2015

I am trying to create a project template for Visual Studio 2015. I created a ASP.NET 5 class library (under the web section of Visual studio 2015) and used the option File->Export template. When I tried to create project using the exported template, only the .xproj file gets created. The files within the template and the dependencies are not getting added. But the same procedure works fine for normal class library project.
Am I missing something.?
I found the way to do this. Open up the zipfile and edit the .vstemplate file. The line that is missing is
<CreateInPlace>true</CreateInPlace>
This line goes between
<TemplateData></TemplateData>
This solves the problem for .NET Core 1.0.1 Tooling Preview 2, VS2015 Update 3.
In order to solve this, I had to extract the output zipfile, and edit the .vstemplate file.
Removing the TargetFileName attribute from the <Project> element under <TemplateContent> worked, and the new .xproj file was correctly named.
The relevant part of the .vstemplate file looked like this:
<TemplateContent>
<Project TargetFileName="MyProject.xproj" File="MyProject.xproj" ReplaceParameters="true">
And I changed it to:
<TemplateContent>
<Project File="MyProject.xproj" ReplaceParameters="true">
The inspiration for this came from looking at the ASP.NET 5 project templates, and specifically the empty web's .vstemplate file.

Open a file in Visual Studio's CSS Source Editor

I am using Phil Haack's T4CSS T4 template based on .less
One bad thing about Phil's solution is that visual studio opens the .less files as plain text files rather than as css files. (Thus no intellisense.)
How can I get VS to open a .less file in the CSS Source Editor?
I've tried:
Right Click > Open With, but the CSS Source Editor isn't listed.
Tools > Options > Text Editor > File Extensions, but once again, CSS Source Editor isn't listed.
Can this be done?
I just posted an extension that does this; you can download it from the Visual Studio Gallery.
The key to the extension is the .pkgdef file:
[$RootKey$\Languages\File Extensions\.less]
#="{A764E898-518D-11d2-9A89-00C04F79EFC3}"
[$RootKey$\Editors\{A764E89A-518D-11d2-9A89-00C04F79EFC3}\Extensions]
"less"=dword:00000028
Note that this extension doesn't do anything to help the CSS language service support Less; nested rules, for example, don't work very well at all (it confuses the CSS language service).
Midscape's Web Workbench Visual Studio Extension adds support for .less files, including syntax highlighting and Intellisense. It also adds support for SASS and CoffeeScript.
If you are using the dotless.Compiler.exe tool (as opposed to the http handler), there is nothing to say your files have to end with .less. I name my suffix my .less files with .css so that Visual Studio treats them like css files. E.g. my naming convention is:
/css/common.less.css ==> /css/common.css
I run the dotLess compiler as a post-build event, e.g.
$(SolutionDir)\packages\dotless.1.1.0\Tools\dotless.Compiler.exe "$(ProjectDir)\css\common.less.css" "$(ProjectDir)\css\common.css"
How to turn on Visual Studio 2010 .css Intellisense on .less file

How to include different Javascript files depending on build configuration?

Is there a way to include a different Javascript file in an ASP.NET page depending whether it's a debug or release build?
For release builds, I want to include the minified (using yuicompressor) Javascript, but I would like to use the more readable file for debug and development.
You can do one of two things ... either rename the appropriate file to the filename referenced in the html in a postbuild step, or dynamically include the html based on the preprocessor symbol.
For development, if you're using Visual Studio 2008 SP1 you can also take advantage of the improved support for JavaScript IntelliSense. Scott Gu has a blog post here:
jQuery Intellisense in VS2008
The basic set up is:
Install Hotfix KB958502
Reference release versions of your .js files as you would normally
Ensure that you have the "development" version of the .js file sitting next to it, called *-vs-doc.js or *.debug.js
This will get you all the intellisense for the files.

Resources