Are there no quick actions when editing razor (cshtml) files? - c#

I'm running a clean install of VS Enterprise 15.5 and have just disabled R# to see what VS is actually capable of lately (and I was tired of it being so slow).
We do lots of work with ASPNet Core cshtml files. I noticed there are no quick actions when editing the razor file. For example:
The editor can see that a #using tag is unnecessary, but there is no quick action (lightbulb) to suggest that it be removed.
Same with a class that is referenced below (Notification), there is no quick action to suggest that a #using tag be added.
In normal .cs files these actions work nicely, and I am a bit surprised not to see this in razor files. Is something not setup correctly?
Resharper had these suggestions and I figured that VS would as well, but perhaps I am wrong?

A new Razor editor in Visual Studio with support for quick actions (Ctrl+.) is under development. The image below shows you how to enable the preview for this feature:
https://devblogs.microsoft.com/aspnet/wp-content/uploads/sites/16/2020/07/enable-experimental-razor.png
You can read more here:
https://devblogs.microsoft.com/aspnet/new-experimental-razor-editor-for-visual-studio/

Are there no quick actions when editing razor (cshtml) files?
Nothing build in for VS-2015 or VS-2017. Although if you just add a web.config and namespaces you don't need any using statements in razor files.

Related

Using Razor View Engine with Nancy, .cshtml not an available extension

I’m working on a project using Nancy on a Mac, and I’m editing my project using a standard text editor (Atom). I would like to use the Razor View Engine, and I’m trying to figure out how.
I included
Nancy.Viewengines.Razor": "1.3.0" in the dependencies in my
project.json file
using Nancy.ViewEngines.Razor; at the top of my HomeModule.cs
file
#inherits
Nancy.ViewEngines.Razor.NancyRazorViewBase<nancytest.Objects.Task>
at the top of the View I would like to use Razor in
(task_added.cshtml), which has an extension of .cshtml
But when I load up the project, I get the error:
Nancy.RequestExecutionException: Oh noes! ---> Nancy.ViewEngines.ViewNotFoundException: Unable to locate view 'task_added.cshtml'
Currently available view engine extensions: sshtml,html,htm```
Is there anything else I should include for it to recognize .cshtml?
Got it to work! Unfortunately, the view engine is built on code that will not run on a Mac. This solution only works on Windows.
I added the following code to my configuration file (Startup.cs), translated from the Razor Engine Wiki.
public class RazorConfig : IRazorConfiguration
{
public IEnumerable<string> GetAssemblyNames()
{
return null;
}
public IEnumerable<string> GetDefaultNamespaces()
{
return null;
}
public bool AutoIncludeModelNamespace
{
get { return false; }
}
}
The line #inherits
Nancy.ViewEngines.Razor.NancyRazorViewBase<nancytest.Objects.Task> at the top of each View is also unnecessary.
Have you read: the Razor Engine Wiki and View location conventions?
Do you have an opportunity to try out your code on Bootcamp, or a Windows machine to see if it's Mac-specific? Presumably you're running on Mono as it's on a Mac, likewise have you got any other projects/ tutorials running on your Mac?
Can you provide more detail on what you have/ haven't tried? Whether you have the tutorial examples working?
At a guess, I'd say your view files (.cshtml) are not somewhere where Nancy is looking for them. What's your folder structure?
Edit
Sorry, I've read it again and obviously Nancy isn't picking up the razor engine (as it explicitly says so). You don't need the using statement in the HomeModule, is's not referenced there.
Have you double-checked the project reference for Nancy and checked that it's been copied into the bin folder?
Enable the diagnostics (Diags) and look in the 'Information' panel, under 'Loaded View Engine'- this will confirm which view engines are loaded, though it will doubtless say the same thing.
Given that Nancy auto-locates view engines by scanning the loaded appdomain and private bin directory for any dlls that reference nancy and auto-registers the IViewEngine types, it strongly suggests that the Razor assembly isn't in the bin folder, or something similar.
I have had the same problem. I am using Nancy self host on a top shelf windows service. My problem was I only has a reference to the core application, but did not have reference to the Nancy.ViewEngines.Razor.dll in my windows service project.
To fix it, I added the Nancy.ViewEngines.Razor.dll refence to my WindowsService project
Here's my story and how I fixed it, in the event that someone has the same issue:
I compiled my Nancy app and it worked fine on my development computer (Windows 10).
I sent the app to another computer via Google Drive, and my client observed the same error as the original poster here on their computer (also Windows 10).
The cause of the issue was Windows blocking the file Nancy.ViewEngines.Razor.dll on my client's computer, because they had downloaded my code from the Internet.
On the client computer, I unblocked the file from the file properties context menu, and the issue was fixed.
P.S. I also unblocked the cshtml files first, but that did not fix the issue, at least by itself.

Visual Studio 2013 only builds HTML changes, not C# changes

I am building a personal website using asp.NET's webforms in visual studio 2013 express for web and am following this tutorial:
http://www.asp.net/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/introduction-and-overview
My page is structured exactly the same as in the website, I have changed some minor stuff to make it my own but the structure in terms of the C# classes and how the interact with the HTML are exactly the same.
I got to section 5 of the tutorial "Display Data Items and Details" and everything was working fine. I've used git a lot in the past so I decided to create a repository for this project so I can access it at work if I feel like.
Suddenly now when I make changes to the C# classes it won't build. It's even stranger because I if I make a change on an HTML file the change is built. In section 3 of the tutorial we learnt how to make the 'product' classes which are displayed on the products page. If I want to change one of the product names for example, when I build the change is not there. Simultaneously I went and changed some info in the HTML for the contact page, IT CHANGES when I build. Why wont the C# changes take effect when I build any more?
I am relatively new to both asp.NET and visual studio. The HTML changes when I build and the C# does not. When I change either I can see in solution explorer that there is a red tick for pending changes. Why would only the HTML pending change be included in the build and not the C#? How do I ensure that the build is actually building the version I see in my editor window?
EDIT
I do not know if I found the original cause but I found a solution/workaround. I realized that the classes mentioned above were grabbed by the html page from the page's database. The .mdf file for the solution was not being rebuilt whenever I cleaned and built so I physically deleted it and rebuilt the solution and voilla my C# changes occurred. I am still fairly new to this whole thing, can someone explain what the .mdf file does and why it wasn't being rebuilt?
Check your .cs files properties on the properties window in visual studio to make sure their Build Action is set to "Compile", Things that are not set to "Compile" do not get compiled. How MSBuild treats project items depends entirely on their build action. CS files default to "Compile" when you make them, but if you changed them yourself that would be why it doesn't update. Also CS files placed in the App_Start folder default to "Content" and they are compiled by ASP.Net when the Application Starts, so if you changed something in App_Start you need to reset the site.
Not exactly sure what is the problem, but I would do a right mouse click on the solution in Solution Explorer->Clean Solution, then do another build and see if that helps.

ASP.NET MVC 5 Alter View Templates

I'm working on the CSS/Design side of an ASP.NET MVC 5 project with a few other guys. I've made the first CRUD Controller, and the views, and I'd very much like to enforce the general design I have laid out for it for any other views, so they can't go around screwing it up (without really looking like they tried to).
Is it possible to alter the views that are autogenerated when you create a controller, or when you click "Add View" from an action, just for this specific project? I've done some searching, and located the templates, but overriding them in my project doesn't appear to be working. It looks like it may be a bit different for MVC 5 than the others.
Any help is greatly appreciated!
Well I was right that it's a bit different in MVC5. This fantastic guy's page tells us the new templates are at
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Web\Mvc\Scaffolding\Templates
and to just add them to a folder in your project called "CodeTemplates" and voila!

Can I minify JavaScript or aspx code in Microsoft Visual Studio 2010

I'm developing a web app using C# and MS VS 2010 IDE. The project has several of my own .js files in the Scripts folder that are designed to run on a client-side browser. While writing them I gave each variable and function in the JavaScript meaningful names and used plenty of comments.
So I was wondering, can I minify those .js files before publishing them (or uploading to a production web server) in Visual Studio 2010?
PS. It would be also helpful to minify aspx files as well...
There are several libraries listed on the NuGet Gallery for minification.
They will all deal with javascript and css, but none will minify the aspx markup.
Hope this doesn't sound like too much of a commercial but I do recommend my own OSS project RequestReduce. It minifies and merges CSS and JS files (or any css or js mime typed file) on the fly at run time (caching them of course). It also generates optimized sprites of most css background images. Currently it does not minify html but that is on my backlog. The features that make RequestReduce stad out from the rest is that it does not require any code changes and extremely minimal configuration. In fact if you are using nuget, there may be nothing more to do than install the package. It also works with CDNs and web farms. It has been adopted by many of the MSDN and Technet web applications (forums, search, galleries). I have seen some page load times cut in half using this. And of course it is absolutely free.
I don't think it will do everything your looking for but I like to use Chirpy. You can find it at:
http://chirpy.codeplex.com/
It is really easy to use to minify js, css, and combine them into one file. It will also minify js and css in the aspx file.
Look at the documentation page and follow the three links (reverse order is better) to see what it can do.

Show missing PartialView as a build error

I've been reorganising our views at work and moving them around to more appropriate locations.
Because we're currently organising many of our views into their own folders within the Views folder, we're having to reference our views using the full path in RenderView.
Is there a way that I can get Visual Studio to throw a build error if the location of the ascx file that the RenderPartial method points to doesn't exist?
There is an error shown at run time however I'd like it to be part of the build process.
Thanks for any advice
View locations are resolved only at runtime so I really don't see how this could be done, unless you write some custom step in MSBuild that will analyze the source code and check the existence of corresponding view files but this will be a challenging task.
MvcBuildViews might help with this:
Compile Views in ASP.NET MVC

Categories