Custom scaffold for controller - c#

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.

Related

How to override MVC "Add Controller"

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/

How to Map Two Project in Single Solution and How to Redirect to One project action to second Project action

I have use two Project under single solution --
PayrollMVC is Name of Solution which contains two Project
1) Payroll
2) Employee
when i Login from Payroll and Redirect to Employee Project Controller action than it does not Call Controller Action and Layout of Employee Project ..Please
Suggest me How to Map Two project and how to Call Action from one Project to Another and
also I Please Tell me What is the issue of not Calling Action and Layout of Employee Project
One solution to call an action from one solution to the other would be to use Server Transfer in the controller like so:
public ActionResult Whatever()
{
string url = //...
Request.RequestContext.HttpContext.Server.TransferRequest(url);
return Content("success");//Doesn't actually get returned
}
Copied from https://stackoverflow.com/a/20913290/201648
This is useful if you want to quickly transfer a handful of controllers to a page on another website using the same URL, e.g. http://example.com/payroll/foo could show the content http://example.com/employee/foo but still appear at http://example.com/payroll/foo. This solution is quick to implement but also hacky, and it doesn't solve you layout issues (please continue reading about areas below for a complete solution).
In terms of not calling the actions and layout, it's because they're in a different project (so the current project doesn't know about them). For this reason you'd probably want to go to the trouble of setting up areas. There is a full guide for areas here:
https://msdn.microsoft.com/en-us/library/ee671793%28v=vs.100%29.aspx
You can setup shared layouts for areas as described in this link:
http://brockallen.com/2012/08/31/sharing-a-single-_viewstart-across-areas-in-asp-net-mvc/
All the routes (for the controllers) can be configured centrally. I'd start by looking at Registering Area Routes and Linking Between Areas from the first link I posted.

Share PartialView between projects in a solution?

I am developing 2 MVC5 websites. They run seperately, but actually related to each other (1 Admin site, and 1 User site). In both site, I have to render a graphical board using HTML table. I am using PartialView with a Model to render it.
Currently, this is my solution structure:
Project.Common (A class library project, contain the model)
Project.Admin (MVC5 project for the Admin site), refering to Project.Common.
Project.User (MVC5 project for User site), refering to Project.Common.
To render the graphical board, I will use BoardData class from Project.Common, which is ok. But with current solution structure, I have to create 2 PartialView in each MVC5 project, which may be hard to maintain later.
Please suggest a good solution for reusing the PartialView. I already know I can write Helper, but that way, I have to write HTML code inside C# code, which is not very good. Is there any way to do this within Razor View?
One of the options you can use is add a link to a file. In this case keep your Partial view in just single project and in second project use Add -> Existing Item, select your partial view from the first project and click on a small down arrow next to the Add button and choose Add as Link. More info can be found on MSDN, see section Add an existing item as a link.
In this case you will have a partial view only in one project and any changes to it will automatically be applied to other project as well.
You should embed your views into the Project.Common dll, a detailed description can be found on https://visualstudiogallery.msdn.microsoft.com/f28290ce-d987-4f91-b034-707031e10ce6

How to specify different AddController code templates for different MVC 3 Areas?

I know how to customize a controller/view template for an MVC project. However, I have a few different MVC 3 Areas in my project and each Area has its own Models, Views and Controllers. Is there a way to specify different code templates for each Area? I tried to place a CodeTemplates folder in one of my Area sub folders but it didn't work.
The idea is, when I right click on any Controller folder in an Area and select Add -> Controller, I want it to use that Area controller template.
UPDATE:
I will be happy with being able to select the controller template I want to use, but still wondering if it is possible to specify CodeTemplates for the different areas.
Looks like MVC Scaffolding might be my only option here. I will show specific solution when I get it working.
Rereading you question, I realized my answer may not be what you are trying to do. Do you wan't the auto generated code in the view to use a different template? Or did you wan't the page to look differently, like with css changes, etc? My answer applies to the latter. If you want to use different code templates, it seems like (according to this) as long as you give them different names you should be able to select the one you wan't to use. You could also probably just remove the ones you don't want from the project then add them back later.
Create 2 files in the folder you want the template to be applied to, one named _ViewStart.cshtml and the other _Layout.cshtml
In the _ViewStart file, add this:
#{
Layout = "~/Views/Reports/_Layout.cshtml";
}
This will point to the _Layout so change the path as needed. In the _Layout file add whatever you want, just like the layout in the Shared directory. And for any other folder you want to use this layout, just add the _ViewStart file to that directory and point it to the _Layout file you want.

Where should I write logic to get menu from database?

I am using Asp.Net MVC 3.0 and I have my _layout.cshtml in Shared folder. Problem is I want to generate menus in this .cshtml which should be loaded from database. But as I understand _layout.cshtml won't have any action etc associated where I can write logic and I don't want to write all this code in cshtml itself. Are there any options to write logic for cshtml within Shared folder?
You can setup a Controller and a view to render the menu and call it inside the _layout.cshtml.
#{ Html.RenderAction("Index", "Menus"); }
Eranga is correct, but let me expand on his answer, to answer your question specifically.
What you can do is create a new controller ("menus" for example), and create an action called default. Have this action return a view, calling it whatever you would like. Now go to your shared folder and add the view using the name you just specified.
Now for the cool part. By default, the MVC framework will look in the controllername/viewname path first, and if it fails it will then look at your shared/viewname path, which is where the view you just created resides! Neat, huh? ;p
Check out http://www.aspnetmvcninja.com/views/view-search-paths for more info on MVC search paths.
#Eranga has given you a good head start on implementing the feature you requested. I think the below two articles will be helpful as well:
Html.RenderAction and Html.Action:
http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx
A sample implementation of Html.Action method with caching:
http://www.tugberkugurlu.com/archive/donut-hole-caching-in-asp-net-mvc-by-using-child-actions-and-outputcacheattribute

Categories

Resources