How to override MVC "Add Controller" - c#

I am trying to create a visual studio extension to create a new wizard or override the current "Add Controller".
I Have the extension made and everything set up, but i cant seem to use any code generators in .net framework, i tried the System.Web.Razor.Generator and the Microsoft.AspNet.Scaffolding.
Does anyone done this before?
Thanks in advance

This should let you add your own template to the wizard so you can easily add replicated templates.
You can also add or edit views as well
So in this article we have seen how to modify the existing code or
adding our own custom code generation for scaffolding column for
generating a Controller class and view in ASP.Net MVC3 using T4 Code
Generation.
https://www.c-sharpcorner.com/UploadFile/b19d5a/modifying-or-adding-the-default-code-generationscaffolding/

Related

How can I spot why the CSS not working in Razor View (model)?

I have build an web application, and I want to delete some record in the database using ASP.NET MVC. But, in the delete Razor View, the CSS is not working, so the page is like this 1. This is the Delete Page of my application 2. I wonder if there are any solutions for this problem. Thank you in advance.
Check this document describe col-sm, and as i notice the CSS classes that you have been used were marked with red, so maybe the version of bootstrap doesn't contain those classes.
the reference:
https://www.w3schools.com/bootstrap/bootstrap_grid_examples.asp

Wondering if we could generate code with T4 Template on a builded solution

I'm trying to figure out if we could generate code with a T4 Template file post-build.
In fact, I'm kinda new to c# and I needed to find a solution where I generate class from Xml file that could be edited by the user at anytime and the first solution that come to me is T4 Template.
Then I render those generated classes on a treeView and the according properties on a property grid.
Now, I think that it is only generate code at pre-build time, but I'm not sure about that since I don't really see a lot of stuff about that on the internet. I'm still pretty sure now that it's not possible.
So, a second problem comes to me : I finished my entire project like that, so if you all have an idea or another solution that could be nice, i'll really appreciate that.
The 'T4Executer' extension has an option to attach the execution of each T4 template to build events. Before, during and after build, or not run them at all.
Doesn't work for Visual Studio 2022 yet though.

Where do I find MVC4 templates for download and how do I install them?

I am somewhat confused. Searched a bunch of ASP.NET related sites but could not find the answer. I must be missing some search word..
I want to find a gallery of MVC4 templates (free/cheap) and apply them to my project. Where do I do that from? I found one codeplex project (http://mvccontribgallery.codeplex.com/), but when I tried applying by copying, there were a bunch of errors (it uses something called SiteMaster, while my project seems to be using something called _layout).
I see a ton of references to people using them, applying them, but I must be missing something very fundamental since I can't even figure out how to find a list of downloadable templates.
What are the simple 1/2/3 steps for finding/getting/installing a template on my MVC4 project?
You should be looking for a suitable HTML template instead of an MVC specific template.
Once you have that, you will need to modify you master template (usually Views/Shared/_layout) to use the HTML and CSS contained within the template you have chosen.
For MVC3 or MVC4 you don't need to download any template. Just install MVC4 in your visual studio 2010 version on higher and then choose option create new project and that is that. See this below screen capture:
Microsoft will provide you the default MVC project architecture or in your term MVC template automatically when you choose any of above option marked in red box.
What do you mean by mvc4 templates? I'm assuming you mean "website template". With MVC you can use any html template, there's no specific template for MVC (that's why you're not finding any of it). You just find any website template and adapt it to mvc context (put css and images under "Content" folder and put html code in _layout and use it like masterpage), not hard to do at all. In fact, so easy to do after you get the idea that no one publish a specific adapted version :)
Follow these steps:
(note: if you have Asp.Net MVC 4, 3 won't install right (for me anyway), so uninstall and install in order).
http://tedgustaf.com/blog/2011/4/installing-asp-net-mvc3-on-visual-studio-2010-sp1/

Custom scaffold for controller

I'm using repository pattern in my ASP MVC4 app and I thought it would be nice to create custom scaffold templates to avoid tweaking code every time I generate something.
I followed some tutorials from the web. I copied the CodeTemplates directory form ProgramFiles to my project (only some of the files) and I cleared the Custom Tool property for each.
I managed to achieve 2 goals:
override the default template for controller: when I go to Controller>Add>Controllers and select "controller with read/write actions and views...." it uses my template.
add a new view template: when I go to SomeViewFolder>Add>View I can choose my new template there.
The thing I was unable to achieve was to create a new template ControllerWithRepo.tt and be able to select it in Controller>Add>Controllers. Even though I have ControllerWithRepo.tt created it doesn't show up on the template drop down list.
I know it's possible to do what I want by hardcoding things in ControllerWithContext.tt , but it seems to be a lame solution. I would like to do it the right way - to pass a repository class instead of DbContext class and generate the controller the way I want.
Right now If I try to put my repository class as dbcontext it fails to perform the scaffold.
To sum up, how can I create my own controller scaffold template for repository pattern?
I was able to override the existing ControllerWithRepository by following Steve Sandererson's post on Overriding the T4 templates.

Generating classes in Visual Studio. Advice needed on approach

Not sure the best way to achieve this and I would like your input.
I would like to generate a View - a ViewModel and another couple of classes.
I don't want to generate a project but just some classes.
Implementing the IWizard can you generate more than one class at one time?
Ideally I would like the user to click on a template in visual studio ,input some names etc... and generate this 4-5 classes for them.
What is the best to achieve this? Any links or suggestions on approach?
Thanks.
I would suggest using a T4 template. The engine's built right in to Visual Studio 2010, and there's a good free editor you can get through the Extension Manager (Tools > Extension Manager > Online > Search for "tangible T4").
As for how to code the templates, here's a good blog post on it which will get you started:
http://www.olegsych.com/2007/12/text-template-transformation-toolkit/
There's some info in MSDN too: http://msdn.microsoft.com/en-us/library/bb126445.aspx

Categories

Resources