Is there a way to prevent Visual Studio 2010 formatting C# code in ASP.NET MVC views? - visual-studio

I'd like Visual Studio to continue automatically formatting my .cs files, but is there a way to prevent it auto-formatting C# code in between the <% %> tags in ASP.NET MVC views because it really makes a mess of it?

As far as I know there isn't a way to turn off the formatting just in ASP.NET files as it uses the rules for C# in Tools -> Options -> Text Editor -> C#.

There is no way to do it and it is a huge time waster fixing up the formatting in .aspx pages over and over again. It drives me nuts.
There are two connect issues/bugs for this. One was marked as fixed for months but the latest response does not look like anything was even done.
https://connect.microsoft.com/VisualStudio/feedback/details/514691/add-aspx-or-inline-c-as-an-option-in-text-editor-formatting-options
https://connect.microsoft.com/VisualStudio/feedback/details/586729/support-c-formatting-options-for-aspx-pages

Unfortunately (afaik), you can't have separate settings for C# in .cs and .aspx files. You can change the settings for C# in Tools -> Options -> C# -> Formatting.
To get things to work well with MVC, I changed some settings in the New Lines section. I unchecked the boxes for control block, object initializers, and "else". This produces the walk-like-an-egyptian style braces that works best in MVC code, without screwing too much with my normal C# code. I leave the settings for functions, classes etc. alone since you don't normally define those in an .aspx file.

You may consider using Razor as your View Engine instead of aspx.
see also:
http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx (Scott Guthrie)
ASP.NET MVC Razor view engine (StackOverflow)

Related

How does one prevent Resharper from reformatting Javascript inside Razor pages?

Using VS2017, I am editing Javascript inside a .cshtml page and the Resharper engine keeps reformatting the code.
I don't see an obvious way to turn this functionality off while keeping the formatting of Razor syntax on.
Am I missing something simple?

MVC Razor GUI and Coder Working Together

I would really like to use the MVC and Razor technology for my web application. The problem I am running into is that I am the coder and will be working with a HTML GUI designer.
I only see 2 ways for the GUI person to work at this point.
Install Visual Studio and open the whole solution and work like I am working, except he would stick to the CSHTML files.
Use his favorite HTML editor or notepad and edit the CSHTML files manually and then also have something like IIS Express installed with the app configured so that they can refresh the page they are working on.
Neither of these seems productive or intuitive. I would love to see the GUI person able to use a tool like Expression Web or other tools that allow you to see the design and html at the same time. Having tools available for shading and colors and positioning would be good.
Now I understand why it is this way. It is because of the processing that Razor does to render the pages. This is most true by the fact that most of the cshtml pages themselves are not complete pages. They are meant to live inside of a _layout page.
Isn't there something, though, that can at least show the basic rendering during editor?
What are other people doing?
You have a few options. AFAIK there is no WYSIWYG HTML editor that understands Razor. So, either your designer must work in straight html, or...
1) The designer works in pure HTML files. They give you those files, and you adapt them to Razor. If they need to make changes, you can track those changes with a diff tool (from the previous version) or use a version control system to compare versions of the raw html. Then you apply those changes to your Razor files.
2) Your designer works in .aspx files, which Expression Web understands. You could convert the WebForm syntax to Razor syntax. Again, you are spent doing lot of work, but probably less work than the pure HTML way of doing things, because the designer will have designed the code for master pages. These can be relatively easily adapted to Layouts.
It would be nice if the next version of Expression understands Razor.

Telerik problems with IntelliSense and Templates

I am working on the project where I use Telerik MVC controls in order to show some NumericTextBoxes and other controls. I have two problems that are maybe related.
1/ IntelliSense works fine in Views/Shared and in some Views. But in some it doesn´t work. The code is absolutely similar without specific blocks of code. I try to add all Telerik namespaces to Web.Config in Views folder, but it doesn´t work. Just in Views/Shared is IntelliSense working.
2/ I would like to use [SourceCodeFile] attribute in my Controller in order to using NumericTextBoxes templates. But VS says "Unknown attribute" and "Resolve" is not in context menu. I was trying to add all Telerik namespaces but it still doesn´t work.
Some suggestions?

How can I get TextMate to recognize intellisense for ASP.NET MVC Razor views?

I'm using TextMate on a Mac as a front end designer and trying to open cshtml (razor) views to just tweak html, but the html intellisense does not trigger. How do i get html intellisense on the cshtml files?
TextMate doesn't have HTML intellisense. It has language-specific tab completion and dumb attribute completion on some languages but nothing that comes close to Visual Studio's "intellisense" or Eclipse's "content assist".
If you want to use its HTML-related features you only have to select HTML in the "Language" menu at the bottom of the window.
That said, I just saved a sample of .cshtml from a random tutorial and TextMate recognizes it as HTML automatically so I'm not sure what's going on.
There are a couple of ASP.NET oriented HTML bundles available, did you try them ?
Here's some information I found on associating file types in textmate. Hopefully it will be of assistance.
https://superuser.com/questions/40506/textmate-file-type-association
http://blog.macromates.com/2007/file-type-detection-rspec-rails
Ultimately, you need to tell TextMate to use the html engine with any file that has a cshtml or vbhtml extension.

Where can I change the Asp.net MVC 3 Razor syntax highlighting in VS10?

I searched Tools > Options, but where in VS10 can I turn off or change the default highlighting for the new Razor view on the #nuggets in ASP.NET MVC 3?
Razor only adds one entry to Fonts & Colors, "Razor Code". That controls the background color given to Razor Code nuggets. The rest of the colors are the same as the C#/VB colors. The "#" character itself and Razor keywords like "model" and "section" use the same color as the ASPX "<%" tag ("HTML Server-Side Script", thanks to another Andrew for digging up the specific entry).
If you'd rather we had separate entries for some of these, please email us and suggest that: razorvs#microsoft.com
The syntax highlighting for razor is based on the c#/VB default highlighting. So you'll have to change those settings to see any changes in a cshtml file highlighting. Might be a good feature request...I don't know.
In Visual Studio 2015
Tools -> Options -> Environment -> Font and colors
The name of the property is HTML Razor Code Background Change the Item background to the color you like.

Resources