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

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.

Related

Imported website into web application in visual studio gives 'Type typename already defines a member'

After importing this existing (and functional) site into Visual studio (tried 2017 and 2019 both) running .Net 4.0 I am getting a plethora of not found errors.
Codebehind pages are not able to see elements on the existing page
Classes are declared to be duplicated or ambiguous
Function names (both standard like Page_Load and custom) are reported as already defined.
I am sure that all these items are related. Here are the steps I followed:
Created new Web Application from Visual Studio 2017 template
Copied solution and csproject files into existing website folder
Added files into the project
Re-added references to site
Set custom class .CS files to Compile
Removed "unnecessary" using statements (which were already declared in web.config)
Create a new ProfileCommon stub referencing ProfileBase
Beyond all this I have tried renaming a number of these functions and classes (using the rename) to create a new reference point.
Not sure what else I can do on this beyond major changes or a complete rewrite (100+ pages of code so far) by creating the new class files then copy/paste.
What other options are open to me?
Ok, this is is a migration project. Only you can "determine" if you want to convert from a web site to a web site application, and the time and efforts for such a migration project.
Now, having stated the above? No question, that I prefer hands down a web site applications. Simple things like have VS compile and build the application, ability to add assemblies (and not have the mess of .dll's HAVING to be in the bin folder to resolve references - yuk!).
And then simple things like the ability to multiple projects in one project are things I come to enjoy, and expect over the years when developing software.
About the "only" thing going for a web site? Your deployment is oh so easy, since you can open a web page, or code behind, hit save, and you are done!
but, this means both source code (code behind) has to be deployed to the web site. As noted, I prefer a build and compile process in VS. This does mean that to make a "minor" change, then you have to do a full re-deploy of the whole site.
However, these benefits I like and enjoy? it not all a given that converting an existing web site to a web site application is worth the efforts. Only you can make this decision. On the other hand, you would not be doing this work unless you KNOW what you doing, and thus can determine if these efforts are worth the time.
Such a conversion and refactoring process is somewhat beyond a simple post on SO.
However some steps and tips are outlined here:
https://devblogs.microsoft.com/dotnet/converting-a-web-site-project-to-a-web-application-project/
I would consider starting over, and try the steps in above.
There is a "convert" option outlined in above, and this can save enormous amounts of time. It has been some time since I have attempted such a conversion, and I not tested the "convert" option in vs2022, and hopefully it still exists.
However, another possible option? Keep the site as web site.

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.

Create a general "skeleton" from a c# project

I've a very complex solution in c# containing about 20 projects, each of them control a device since it is a driver.
In many cases those projects use similar structures/code (for example everyone as a connect method, a retrive data method and so on).
Is there a tool to analyze the code and create a general "Skeleton" that can be reused?
You can easily create a project template from an existing project from the File>Export Template menu. The process is described in How To: Create Project Templates.
There is no tool that can decide what to include in a skeleton project, as this depends on knowledge of what each project actually does, which parts that can be generalized and which have to be project specific.
You can use duplicate analysis in Visual Studio or Resharper to find repeated code, but this won't tell you what should be in a template and what shouldn't.
What you can do, is:
Extract common functionality in a separate project that all device projects will reference
Create a template from one of the device projects.
Use template parameters to customize the resulting template.
Step #1 will result in a much simpler template, that is easier to customize

Difference between "codebehind" and "source"

Can any one tell me with example what's the difference between Codebehind="MyCode.aspx.cs" and Src="MyCode.aspx.cs"?
CodeBehind
Specifies the name of the compiled file that contains the class
associated with the page. This attribute is not used at run time. This
attribute is used for Web application projects. The CodeFile attribute
is used for Web site projects.
Needs to be compiled ( asp.net 1.1 model) and compiled binary is
placed in the bin folder of the website. You need to do a compile in
visual studio before you deploy. Good model when you do not want the
source code to be viewable as plain text ... for example when
delivering to a customer who you not have obligation to provide code.
Src
Specifies a path to a source file containing code that is linked to
the page. In the linked source file, you can choose to include
programming logic for your page either in a class or in code
declaration blocks.
You can use the Src attribute to link build providers to the page. For
more information, see the BuildProvider class. Also, in versions of
ASP.NET prior to 2.0, the Src attribute was used as an alternative way
to link a code-behind file to a page. In ASP.NET 2.0, the preferred
approach to linking a code-behind source file to a page is to use the
Inherits attribute to specify a class, along with the CodeFile
attribute to specify the path to the source file for the class.
You provide the source file with the solution for deployment. ASP.NET
2.0 runtime compiles the code when needed. The compiled files are at Microsoft.NET[.NET version]\Temporary ASP.NET Files.
Its always recommended to check the official documentations first. See the msdn documentation for this Question.
codebehind is a particular particular technique born with NET.
This allows you to write application code by separating it from the graphical presentation.
For example:
<script runat="server" src="MyCods.cs" />
execute a primitive code-behind. this because the code isnot compiled and not generate. For This reason remain a simple format plain text.
Il codebehind is default applied in the major IDE as:
Microsoft Visual Studio 2003 - .net 1
Microsoft Web Matrix - .net 1
Microsoft Visual Studio 2005 .net 2
Microsoft Web Developer Express .net 2
This implies that the association of the source code, in the form of DLLs, the page to which it is related. I remind you that each page is viewed. NET as a single class, which will inherit the code derived from. Previously compiled dll.

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