ASP.NET MVC 5 Alter View Templates - c#

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!

Related

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

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.

adding view is disabled in VS 2015 MVC 5

I have a working MVC 5 web application. everything was ok until suddenlly i found out that i cannot add a view to the application and the add button is disabled.
I use VS 2015 Entrprise update 3 RC.
C# as a programming language.
the project type GUIDS tag looks like this
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
please help me with this.
Thanks all
Your ProjectTypeGuids look ok. You can view a list of ProjectTypeGuids here.
I have Visual Studio 2015 Community Edition here and it works fine when I try to add a View clicking on Views folder and an Action inside a Controller.
There are some similar questions here at stackoverflow, maybe they can help you:
Cannot add view to ASP.NET MVC 5 project
Visual Studio 2013 button to add a View is disabled
Can you give more information about how you are trying da add that View please?
Some tests
I can reproduce clicking on the Views folder and the add button is disabled if I choose Template: Empty and don't select any Model Class:
The only way the button Add will be enabled with no Model Class selected is if I choose Empty (Without Model):
Make sure you have all the following installed:
Modifying VS -> workloads -> installation details

Can I write a class within an ASP.NET site without compiling a DLL?

I'm writing a small website using ASP.NET, writing the C# into the .aspx file directly without using code-behind or compiling a DLL. I do this mainly to allow small edits with just a text editor without having to keep going back to Visual Studio.
This is working great, except I want to write a class for some code that will be used by multiple .aspx pages. Is there a way I can add a .cs file in the same way I have C# code in .aspx files?
Good question.
I assume you have a "Web Site" in Visual Studio. If not then perhaps you should invest the time to create a new Web Site and migrate the pages into it. Then publish it and that will make things easier. The problem you describe here will be solved plus other things. If you modify the web site outside of Visual Studio however then that would probably be a problem too. I am a beginner but you probably can open the site live for editing directly. I am not sure.
I was curious about how to do what you are asking so I explored.
Anyway, assuming you already have a Web Site in Visual Studio, use "WebSite" | "Add New Item" to add a class. VS will ask you if you want to create an APP_CODE, say yes. The cs file won't have a namespace, the class will be in the global namespace.
When I got that far I was able to use VS to test a page using the class. When I copied the source, IIS said it could not find the class. So be sure that the APP_CODE directory is in the root of the web site. That probably is not enough so we will continue.
I published the web site. The published web site worked, it found the class. I could not figure out what the difference is between the published site and what I copied over. So I studied the output of the publish and I see it is creating virtual directories and adding ACLs.
I think this is the trick. First be sure the APP_CODE directory is in the root of the web site. Then go into the IIS Manager and find the web site. Right-click and select "Convert to application". When I did that the files that I just copied worked.
If you publish the site using Visual Studio then it will do that for you. You only need to do it once.
I tried a few other things so there might be something else I did that is necessary but I do not think so.
Add something like the following at the top of your .aspx file -
<%# Import namespace = "mydll" %>
This will ensure that the class is accessible in the mark-up, assuming the methods and properties etc are setup to be so.
Any changes to the dll will still require you to edit and build in VS however.

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.

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