ASP. net mvc Tip #16-create an ASP. net mvc macro operation

Source: Internet
Author: User

ASP. net mvc Tip #16-create an ASP. net mvc macro operation
ASP. net mvc Tip #16-create ASP. net mvc macros

Original American: http://weblogs.asp.net/stephenwalther/archive/2008/07/09/asp-net-mvc-tip-16-create-asp-net-mvc-macros.aspx
Http://www.cnblogs.com/mike108mvp

Note: The translation level is limited. If there are any mistakes or mistakes in the translation, you are welcome to criticize and correct them. Thank you.

In this post, I will demonstrate how to create a visual Studio 2008 macro operation (macro) to use a command to create a new MVC controller ), view folder and controller unit test.

Don't get me wrong. I like Visual Studio 2008 design tools. I like to drag and drop projects from the toolbox of Visual Studio 2008. I have remembered many shortcut keys for Visual Studio. However, after today, nothing is faster than entering a command in the Command window.

In this post, I will show you how to use Visual Studio macro operations and Visual Studio command windows to generate files andCodeUsed in ASP. net mvc projects. In particular, I will show you how to create a macro to generate a new MVC controller, MVC view, and MVC controller unit test.

Create a Visual Studio macro

You can create Visual Studio Macros in two ways. First, you can record your actions when using Visual Studio. To record macros, you can choose tools> macro> record temporarymacro ). The control panel of this Macro Tool looks like a video recorder. After recording, you can click the stop button, and then you have a new macro. You can save it and play it again later (replay.

Another way is to write a macro from the beginning. I generally use this method to create an MVC macro. Previously I was forced to use this method only to create macros, because I need to write macros for general functions so that they can be used in any MVC project.

Visual Studio macros must be written in VB. NET. (Translator's note: It's really depressing ). Most macro code includes calls to the Automation Object Model of Visual Studio. My MVC macro uses the following automatic object:

DTE-Visual Studio specifies the highest level object of the Automatic Object Model. This object has a solution attribute that represents your Visual Studio solution. You can use the solution attribute to contact a separate project in your solution.

Project-represents a separate project in your solution, such as an MVC project or a test project. Use the projectitems attribute to contact certain content items (such as files and folders) in your separate project.

Codemodel-represents the code in a project. You can use codemodel to add a new class to a project.

To create a visual Studio macro project, you can choose tools> macro> new macro project. Select this menu to open a dialog box for creating a new macro project (see figure 1 ).

Figure 1-new macro project dialog box

After you create a new macro project, the macro Explorer window opened in the address is usually used by the Solution Explorer window) occupied (see figure 2 ). You can double-click any macro in the macro browser window to run it. You can also right-click a macro to edit and modify it.

Figure 2-macro Explorer window

When you edit a macro, the integrated development environment (IDE) of the macro will appear. Macro ide looks like another Visual Studio 2008 instance. However, it is a development environment with more limited functions designed specifically for macro development.

You can create a bunch of macros by writing the VB. NET code module. Every public sub-user defined in the moduleProgram(Public subroutine) is exposed as a separate macro. For example, the super simple macro code in code listing 1 only displays a "Hello world !" The dialog box is displayed.

Listing 1-test. VB

Note: This code is not easy to paste. Check the code in the original article:
Http://weblogs.asp.net/stephenwalther/archive/2008/07/09/asp-net-mvc-tip-16-create-asp-net-mvc-macros.aspx

 

A macro subroutine (macro subroutine) can receive parameters. However, all parameters are optional parameters (optional parameters)-defined using the optional keyword of VB. If you do not set the parameter as optional, This subroutine will not appear in the macro browser window.

Execute the Visual Studio macro

When you create a macro, You can execute it in several ways:

· Macro browser window
· From the command window
· Find input box)

The easiest way to execute a macro is to double-click the macro in the macro browser window. However, if this method is used, the goal of this post will be lost. The purpose of this post is to explain how to quickly modify an ASP. net mvc project by entering commands.

The second method is to execute a macro from the command window. Choose View> other Windows> command window to open the command window. (The better way is to use the CTL-Alt-A shortcut to open the command window ). After the command window is opened, enter the macros. Macro macro name to open the macro. For example, you can input macros. MVC. Test. sayhello in the Command window to execute the macro created in the previous section.

Macros. MVC. Test. sayhello

When you enter a macro name, the complete macro name is automatically filled (see figure 3 ). Therefore, you only need to enter m, and then select the macro you want to execute from the automatically displayed drop-down list.

Figure 3-command window statement completion

 

I found the last method to execute macros from the blog of Sara Ford.

Http://blogs.msdn.com/SaraFord/

You can execute a macro from the find input box in the Visual Studio toolbar (see figure 4 ). Input> symbol to select a macro for execution. You can use the CTL ++ shortcut to quickly navigate to the find input box.

Figure 4-executing a macro from the find input box

Create a macro alias

If you want to reduce the number of input characters when executing a macro, you can create a macro alias. Use an alias like the following:

Alias H macros. MVC. Test. sayhello

After you execute the preceding command in command window, if you want to execute the sayhello macro, you only need to enter the letter H.

You can use/Delete to delete an alias:

Alias H/delete

By using aliases, you can enter at least characters to execute common ASP. net mvc tasks.

Create an ASP. net mvc macro

The MVC macro code is included in code list 2. Code List 2 contains a generate code module, which contains four public subroutines: All, controller, view, and controllertest.

Listing 2-Generate. VB

Note: This code is not easy to paste. Check the code in the original article:
Http://weblogs.asp.net/stephenwalther/archive/2008/07/09/asp-net-mvc-tip-16-create-asp-net-mvc-macros.aspx

 

You can execute the following code in command window or find input box to create a new controller, view folder, and Controller Test Project.

Macros. MVC. Generate. All product

Run this command to create a new controller named productcontroller, a view named product, a view folder of product, and a unit test project named productcontrollertests.

If you want to reduce the amount of input code for executing these commands, you can create a macro alias like this:

Alias Gen macros. MVC. Generate. All

This command creates an alias named Gen, which represents the MVC. Generate. All macro. After creating this alias, you can create a new controller, view folder, and unit test project like this:

Gen customer

This is probably the fastest and lazy ultimate killer to create an MVC project. After executing this command, your solution window will contain the files shown in Figure 5.

Figure 5 -- generating a customer

Summary

The purpose of this post is to describe how to create commands from Visual Studio to quickly generate ASP. net mvc project files. By taking advantage of this powerful automated model that is included in Visual Studio 2008, you (theoretically) can create an ASP. net mvc application without opening a separate file
Dialog box.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.