Code generation: Custom controller templates - asp.net-mvc

To start off, I'm using Asp.Net MVC 4, and I've modified my solution to contain the CodeTemplates folder by following this tutorial. (I think my issue might be MVC related?)
I've replaced the code in the Ajax grid template with a with my own JQgrid-template, which was fine for getting my project started. But I'm having trouble creating completely new controller templates (well the having them show up in the create new controller menu)
any ideas?
Update
I've taken a few stabs at using the scaffolding feature, but it's not exactly what i had in mind (although it does work like expected. I watched a video by steve sanderson on the topic that was quite informative), any way to get my custom views/controllers inside the add controller gui?

As far as my understanding of your problem goes, you cannot manage two controllers within one .ps1 file, which corresponds to your "AddController" directory.
You have to use the Package Manager and issue a command like
Scaffold CustomScaffolder <choose-name>
This will generate another subfolder below CodeTemplates with the name . From there, you open and edit t4 files like you did with your existing one.
More info here

Related

Entity Framework + MVC 4 + Knockout.js AddView templates?

I've been messing around with knockout.js a little bit lately and it seems like it has a lot of promise. However, I can't seem to find any good Code Templates (aka t4 templates or .tt files) for it.
My GUESS is that the only changes that we would need would be on the view side, not the controller side, but I'd be up for looking at new controller templates too.
Where I am right now:
Create a new MVC 4 project
Using NuGet, install entity framework 5, jquery, jquery ui, and knockout
Create a .edmx model by reverse engineering your existing database with Entity Framework
Drag C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 4\CodeTemplates from Explorer into the root of your project
Delete CodeTemplates\AddController
Highlight all of the items in CodeTemplates\AddView\CSHTML and blank out the text in "Custom Tool" in properties to keep them from
building
Start screwing around with Create.tt and Edit.tt to add in knockout code.
Goal Expected Input: Right Click > Add Controller ... > MVC Controller using EF
Goal Expected Result: Controller & View created using EF with knockout code already in place.
If you use something similar to knockout that you like more, I'd love to hear about it!
Also, is it necessary to use addons like MVCScaffolding? It seems like you could just customize the existing templates and get the same results.
Thanks for your time!
I would go the MVCScaffolding route. You could create your own custom scaffolding script whenever you wished to add a controller, that is, as long as you can stomach PowerShell.

Are shared views still around in MVC 4 RC?

I'm brand new to MVC and I was trying to work my way through the music store tutorial:
http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-3
I could tell right away that things looked slightly different in MVC 4 RC but one big thing I'm hitting is I don't see any Shared View being created when I add a Home View. In fact I don't see a _Layout* file anywhere within the project.
So my question is, has the shared view thing been replaced by some other mechanism or do I need to do something else to add this explicitly?
Thanks,
JT
From http://blogs.msdn.com/b/jasonz/archive/2012/05/31/announcing-the-release-candidate-rc-of-visual-studio-2012-and-net-framework-4-5.aspx:
If you’re doing web development, you’ll notice a handful of updates in
the RC. First off, we’ve updated some of the project templates. We
made the MVC4 Empty template truly empty, and renamed the original
empty template to now be called the “Basic” template. We also improved
the performance of the New Project creation time.
Essentially all of the initial templates have been cut down a lot. The Empty project doesn't come with a HomeController either - you can add anything you need back.
Apparently this is tied to how you start the project. If you select Internet Application when you create a new MVC project it will build these files for you but if you select Empty you have to create them manually.

What are the steps for converting the view engine for an MVC project from .ASPX to Razor?

I've inherited an MVC3 project that has a large number of ASPX views that I would like to convert to Razor. This question => Aspx to Razor syntax converter? is similar to mine, and it helped me find a bunch of options for converting the views themselves, but I'm unclear on the steps I need to take in addition to converting the views.
The first known limitation of Telerik's razor-converter is "The tool only works with views and does not deal with the project structure and master pages". This tool claims to be able to convert master pages as well, but it doesn't look like anybody beyond the developer has ever used it.
I think these are the steps I need to take:
Use a utility to convert the views
Convert the master pages manually (how do I do this?)
Modify the project structure (what needs to be modified?)
Delete the ASPX files
Test the application (any specific gotchas I should look out for?)
Are these the right steps? Can you help me with my questions on steps 2 and 3?
I have only tried this on one solution and the actual conversion did a fairly good job. I downloaded the Telerik converter project, compiled it, and then converted my projects using these command lines:
aspx2razor C:\Development\MyProject\MyWebProject\*.ascx C:\Development\MyProject\MyWebProject -r
aspx2razor C:\Development\MyProject\MyWebProject\*.aspx C:\Development\MyProject\MyWebProject -r
aspx2razor C:\Development\MyProject\MyWebProject\*.master C:\Development\MyProject\MyWebProject -r
I only needed to go back to add an #include for a namespace here and there, and to add a few parenthesis to force the Razor view engine to recognize my inline code properly. This was also a fairly simple solution, so YMMV. But even if it converted 80-90% of your views successfully, it's that much less manual work which you would have to perform yourself. From here, you could also create a _ViewStart.cshtml file and make a few minor adjustments to take advantage of Razor-specific layout features. (Check out Scott Gu's post on it here: http://weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx)
The big issue I had was trying to reconcile the file changes with source control. Since the classic MVC view engine uses .aspx, .ascx, and .master extensions, I had to manually add the .cshtml files to my MVC web project and source control then remove the old versions. It wasn't difficult, just time-consuming.
In addition, you may need to add all of the necessary web.config entries to support the Razor view engine as well if your project was created using MVC 1 or 2. Projects created with MVC 3 should already have these entries in place, even if it was not originally created as a Razor site.

Adding Asp.net mvc to existing web app how to get scaffold options

I followed the guidance in the Professional Asp.net 1.0 Wrox book for adding the MVC references to an exisiting web application and it works well except for the scaffolding options. When i right click a controller i do not get the scaffold view options that you get in a new asp.net mvc app. I am sure there is a .csproj hack that is needed to get the scaffold options but i can't find any references anywhere. Has anyone else run into this and found a solution?
Well google to the rescue. I found an answer at the following blog: http://wildermuth.com/Tag/ASP.NET+MVC
You need to edit the .csproj file and add to ProjectTypeGuids {603c0e0b-db56-11dc-be95-000d561079b0}. Order seems to matter. Originally i added to the end of the guid list and the project would not load and threw an unsupported error. I created an empty mvc site and looked at the guids and the guid above was listed 1st. So i added it to the beginning and everything worked fine.
Have you looked at how you register the data context in you Global.asax file?
This link may have more details that can help:
http://msdn.microsoft.com/en-us/library/system.web.dynamicdata.metamodel.registercontext.aspx
and this:
http://msdn.microsoft.com/en-us/library/system.web.dynamicdata.metamodel.aspx

ASP.NET MVC RC - Creating a MVC User Control with a codebehind

Trying to create a MVC User Control in the Release Candidate and I can't see to make one with a codebehind file. The same is true for MVC View pages.
Creating Views in the Beta would produce codebehinds...am I missing something?
Code behind kind of defeats the purpose of the MVC Framework. Functionality should be kept separate from the view, the MVC team felt that code behind pages went against this ideology and therefore removed them.
Your can create a custom helper method to create your control. Also I'm not sure if MVC has view components (Monorail/Castle) but that could be an option as well.
From ScottGu's Blog post:
*Views without Code-Behind Files
Based on feedback we’ve changed view-templates to not have a code-behind file by default. This change helps reinforce the purpose of views in a MVC application (which are intended to be purely about rendering and to not contain any non-rendering related code), and for most people eliminates unused files in the project.
The RC build now adds C# and VB syntax support for inheriting view templates from base classes that use generics. For example, below we are using this with the Edit.aspx view template – whose “inherits” attribute derives from the ViewPage type:
One nice benefit of not using a code-behind file is that you'll now get immediate intellisense within view template files when you add them to the project. With previous builds you had to do a build/compile immediately after creating a view in order to get code intellisense within it. The RC makes the workflow of adding and immediately editing a view compile-free and much more seamless.
Important: If you are upgrading a ASP.NET MVC project that was created with an earlier build make sure to follow the steps in the release notes – the web.config file under the \Views directory needs to be updated with some settings in order for the above generics based syntax to work.*
I answered this question here:
How to add a Code-behind page to a Partial View
Seems this wasn't particularly tricky, and is quite do-able
This answer worked for a Partial 'ViewUserControl' but the same should apply
Ok.
First: Add a Class file with the convention of .cs (i.e. view.ascx.cs)
Second: Add "using System.Web.Mvc;" to the class
Third: Change the Class to Inherit from "ViewUserControl<>"
Fourth: Add the following to the View's header:
CodeBehind="View.ascx.cs" Inherits="Project.Views.Shared.View"
Fifthly: Copy the files out of the solution and drag back in to reassociate the two together
Note: For this to work with a Normal MVC View you just need to inherit the class from "ViewPage"
The whole idea for ASP.Net-mvc was to get rid of the codebehind files...thats why asp web controls didnt matter that most didn't work.But with the changes of getting rid of the code behind comes with a different programming style..The idea is codebehind files are EVIL:
http://stevesmithblog.com/blog/codebehind-files-in-asp-net-mvc-are-evil/
the whole idea is to make sure people remember they are using asp.Net-mvc and not asp.et web pages. take alook at this link ,it explains it a little better:
http://blog.lozanotek.com/archive/2008/10/20/Visual_Studio_Templates_for_MVC_Views_without_Codebehind_Files.aspx
I think this tutorial is what you are asking.. but not really sure what you want..

Resources