How to define a MVC shared EditorTemplate in an external assembly? - c#

I'm trying to reuse shared templates in my MVC applications. For this, I use the Razor Generator VS Extension and I set RazorGenerator as the Custom Tool for my *.cshtml files. In my solution, I've created another MVC project (as described on http://razorgenerator.codeplex.com/) and therein, under Views/Shared/EditorTemplates I've defined a String.cshtml template.
In my actual MVC app, I've added a reference to this project and in a view I try to do the following:
#Html.EditorFor(d => d.FeedName)
(FeedName being of type string). However, the String.cshtml does not get picked up.
I've tried to set the Custom Tool Namespace of String.cshtml to use the same namespace like the actual app just to see if that would help, but it didn't.
So, how can I tell the engine to pick up the editor template from another assembly?

Related

Creating a new Blazor project for components and pages

I am creating a Blazor WASM project and would like to separate my Blazor components and pages from the hosting model.
This would make it easy to change hosting model to for example a Server based application, where I just would need to create a new project and reference my components and pages library.
It would also allow me to separate my components and pages library from specific implementations (services or data models) by using interfaces and DI. Only the hosting project would need to know my implementation details.
The issue is that I cannot get it to work properly with visual studio. I have tried to create a .NET Standard 2.1 project for my components/pages but then I cannot create new razer components. Do I need to configure the project in some way? How should I make my shared components/pages library?
You need to select a Razor Class Library template:
It will contain a wwwroot directory where you can put your images, css or other static web resources. You can also use Library Manager (libman.json) if you add the json.
More information here: https://learn.microsoft.com/en-us/aspnet/core/blazor/class-libraries?view=aspnetcore-3.1&tabs=visual-studio

VS2017 Community Create New Scaffolding

I want to develop my own custom scaffolding in VS2017 Community for Controllers and Views in an MVC5 project (not using .NET Core), but I am having a little trouble with it.
I've copied the templates from this location into my project under CodeTemplates:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\Microsoft\Web\Mvc\Scaffolding
However, I don't want to modify the existing templates, but create my own new ones.
If I try to add a t4 template under CodeTemplates like MyController\Controller.cs.t4 or AddController\MyController\Controller.cs.t4, it will not show up as an option in the menus if I try to right-click on my Controllers folder and add a new Controller or New Scaffolded Item.
Firstly, if I build something like Controller.cs.t4, how do I create a new file from it as say HelloWorldController.cs? Can't seem to find where to look. These are good articles, but I'm having trouble finding what to do about having a t4 template generate a different filename like VS does.
https://msdn.microsoft.com/en-us/library/dd820620.aspx
https://msdn.microsoft.com/en-us/library/bb126478.aspx
What I would like to basically do, is right click on a Model, click a context menu button in VS, and have it call some code I would write that looks at the public functions of the Model via Reflection and procedurally generates the Controller and Views from it using my own templates and places them in the appropriate subfolders.
I may also want it to involve a dialog with other controls, much like the existing Scaffolding menu options work when using an EF Context. Looking at the existing templates that involve EF Context they call this line:
<## ScaffoldingAssembly Processor="ScaffoldingAssemblyLoader" #>
I'm not sure where that sits and how to create my own and reference it is the question.
I may also want an entirely separate template implementation of what exists under CodeTemplates\MvcView that may become a partial view of an SPA page, allowing me to call the CRUD methods via AJAX instead of having it on its own page.
This is already getting a little bit out of hand, and I'm unsure whether this is actually one question or multiple. I'm asking for basically one essential function, but it seems to be composed of several questions: how to process a T4 C# template at design time into a new filename, how to do so with some code that VS understands (does this require VISX knowledge?) and generate the necessary dialogs to set the parameters and run the necessary T4 templates to create files, and how to add that to the standard MVC project right-click context menu (is VISX the solution for this?). I keep asking about VISX because when I look "Custom Command" in a VISX project it's saying it wants to add it to the Tools menu, I don't want it there but on Project's context menu the same way as Controllers, is that only a matter of running Tools -> Customize?
I'm just getting started looking into VISX right now, very excited about it, but there's lots to cover of course.

How T4MVC add-in and ASP.NET MVC integrate & work together?

I have an open source ASP.NET MVC application there is an add-in called T4MVC and I can see all the html, aspx, images, Controllers, views files are integrated, I wonder how it works.
If I create a file(aspx,html...) has it to be integrated manually to that template(T4MVC) or by default it get mixed? could someone please advise of how to get this(T4MVC) ans MVC do work together since I am new in ASP.net MVC.
T4MVC is a template that Visual Studio will compile and execute when you save the file in the editor (there are tools to automate this; search for AutoTT or Chirpy to check them out).
The template scans your project to find controllers, views, images, etc. and generates code that allows you to reference those items programatically elsewhere in your code. Whenever you add, remove or rename a resource (any of the items the template looks for) the template must be executed again to regenerate the code output.
The advantage of T4MVC (over the use of strings to reference things) is that you get compile-time checking, as well as intellisense and refactoring support.

Including xib(s) / nib(s) in a MonoTouch Binding project

I'm trying to create a re-useable MonoTouch library that contains Views defined in a xib, and Controller code written in Objective-C.
I have created a static Objective-C library that contains the relevant controller code with all the outlets declared.
A static *.a library obviously can't contain xib/nib data (if this is possible can someone please let me know), so I can't embed the xibs/nibs in this library.
I have created a 'MonoTouch Binding Project' that defines the relevant wrapper classes.
This is where I would ideally also embed the xibs/nibs, and have them included in the app bundle of any final project that links this dll.
Now looking at what a 'MonoTouch Library Project' does with xibs - it compiles it to a nib using ibtool, and then embeds it as a resource in the resultant library dll using the /res option of smcs. I'm assuming this is triggered because the xib file is marked with a "InterfaceDefinition" build action in the project.
However a 'MonoTouch Binding Project' has no "InterfaceDefinition" build action. Is this possible at all using a MonoTouch Binding Project?
I haven't tried it yet, but I'm assuming I could get it working manually by combining what the Library project does with ibtool and smcs /res and what the Binding Project does with btouch and smcs. But I'd rather avoid this, I'm enjoying the lack of Makefiles lately.
Xamarin/MonoTouch team - any plans to add this to Binding Projects in the future? Any way to force it to work currently?

Visual Studio, Razor, BuildProviders and Intellisense

I'm trying to get Intellisense working for razor views in a non-ASP.NET project and would like to understand the relationship between VisualStudio's Razor editor and BuildProviders.
For background, I'm writing a framework on top of Manos (mono web server) that uses Razor for its view engine. I've got that part working perfectly, but Intellisense in VS doesn't work giving a range of errors from unknown types to unregistered build providers, depending on where the output DLL's of the project are placed.
My project is a .NET Class Library, with .cshtml files (build action none). The base razor view class is defined in a separate assembly (outside the project) which could be registered in the GAC, but currently isn't.
I've already read these articles:
http://www.west-wind.com/weblog/posts/2011/Jan/12/IntelliSense-for-Razor-Hosting-in-nonWeb-Applications
Need razor view engine auto-complete to work in a class library?
http://blogs.msdn.com/b/webdevtools/archive/2011/01/20/how-to-get-razor-intellisense-for-model-in-a-class-library-project.aspx
.NET - Razor outside MVC application - Problems with removing #inherits and providing #model
Sounds like I need to write my own BuildProvider, but can't find any documentation explaining the relationship between a build provider and razor intellisense.
Razor intellisense is flaky at best currently. However, if you're using VS SP1, its slightly better. The web.config workaround (as pointed in your third link) works for me in a class library as long as the extension is cshtml (haven't tried vbhtml so can't say for sure).
Also take a look here: http://razorpad.codeplex.com/
Similar to LinqPad, this will allow you to test your razor code ahead of time.
The Razor editor is pretty heavily tied in to the ASP.Net runtime, in fact it actually runs ASP.Net in the background in order to collect the necessary run-time information.
My only suggestion for getting true-fidelity IntelliSense is a bit of a super-hack. Rather than a Class Library, you could make your application a Web Application Project. A WAP is actually just a class library which VS can host a website from. if you clean out ALL the extra stuff (Global.asax, web.config, etc.) you may get exactly what you're looking for. It's a workaround, and a bit of a stretch at that but give it a shot, it may just work :)

Categories