ArticleDirectory
- Target
- Introduction
- Automatically generate the module code structure
- Modify the module list file
- Add route
- Create a controller)
- Create View)
- Enable Module
- Modules
- Conclusion
I have been studying orchard for nearly two weeks, although I am still vague about many concepts. However, I saw an article on the orchard official website about creating the hello World module. I still can't wait to try Orchard module development first. (Small experience: Learning Open-source frameworks is faster than simply learning them .)
Address: http://www.orchardproject.net/docs/Building-a-hello-world-module.ashx
(The following content is not completely translated from the original article. This article uses orchard V1.1 as an example) to create a module that only displays the hello World on the page. You can enter http: // localhost/helloword in the browser to access this page. Introduction orchard is built on the ASP. net mvc framework, and the module in orchard is equivalent to areas in MVC. MVC is a design model for system division. It divides a software system into three basic parts: model, view, and controller ). M is data, C is the Controller responsible for processing user interaction, the Operation Model and the final view used to display the UI, V is the view only used to present data.
In the original example, the text to be displayed is not transmitted to the view as a model, but the text of "helloworld" is directly written in the view. Therefore, the original text does not care about the model. In fact, we can also process the model in the text to be displayed.
According to the requirements in this example, we need to establish a route to let the system know the Controller action to be executed when there is an http: // localhost/helloword request. Of course, we also need to create this controller and implement the corresponding action to pass the "helloworld" text to the view function. At the same time, we also need to create a corresponding view to display the model passed by the Controller. Automatic Generation Module Code The structure can be automatically created by using a code generation function in Orchard. For more information about code generationd, see command-line code generation or command line tools in Orchard.
After code generation is installed and enabled, enter:
Codegen module mycompany. helloworld
Note: When creating a module in orchard, it is best to use the "company name + function name" method as the module name. In this way, modules of similar functions developed by other companies will not conflict with those of modules in the future.
After the module is successfully created, you can view the module project we just created in the Module Directory of orchard.
The modified lele.txt file can be found in the newly created le.txt file, which is a module list file. He describes some basic information about this module, such as the name, author, and version. This information will be displayed in the system, telling the administrator about the role of this module and other related information.
Module.txt
Name: mycompany. helloworld
Antiforgery: Enabled
Author: Twenty-four painters
Website: http://esshs.cnblogs.com/
Version: 1.0
Orchardversion: 1.0
Description: This is a simple module for displaying helloworld.
Features:
Mycompany. helloworld:
Description: This is a simple module for displaying helloworld.
Note: If this file contains Chinese characters, you need to save it as Utf-8 encoding format.
Add a routes. CS file in the module root directory to define the ing between URLs and controllers. Because there is no corresponding code generation command, you can only manually enter the following code. Routes. CS
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Web;
Using Orchard. MVC. routes;
Using System. Web. Routing;
Using System. Web. MVC;
Namespace Mycompany. helloworld
{
/// <Summary>
/// Define the routing used by the module. To define the routing in orchard, you must implement the irouteprovider interface.
/// </Summary>
Public Class Routes: irouteprovider
{
# Region Irouteprovider Member
Public Ienumerable < Routedescriptor > Getroutes ()
{
Return New []
{
New Routedescriptor
{
Priority = 5 , // Priority)
Route = New Route
(
" Helloworld " , // The URL mode of the route.
New Routevaluedictionary // The value to be used when the URL does not contain all parameters. Index action in homecontroller is executed by default.
{
{ " Area " , " Mycompany. helloworld " },
{ " Controller " , " Home " },
{ " Action " , " Index " }
},
New Routevaluedictionary (), // A regular expression used to specify the valid values of URL parameters.
New Routevaluedictionary {{ " Area " , " Mycompany. helloworld " }}, // Transfer to route ProcessingProgramHowever, it is not used to determine whether the route matches the custom value of the specific URL mode. These values are passed to the route handler for processing requests.
New Mvcroutehandler () // The object that processes the route request.
)
}
};
}
Public Void Getroutes (icollection < Routedescriptor > Routes)
{
Foreach (VAR routedescriptor In Getroutes ())
{
Routes. Add (routedescriptor );
}
}
# Endregion
}
To create a controller, enter the following in the orchard command line: Codegen controller mycompany. helloworld homecontroller After successful creation, a homecontroller. CS file will be added to the module controllers directory. Open this file and modify the Code as follows:
Homecontroller. CS
Using System. Web. MVC;
Using Orchard. localization;
Using Orchard;
Using Orchard. themes;
Namespace Mycompany. helloworld. Controllers
{
/// <Summary>
/// You need to add a themed attribute. Otherwise, the displayed content will not be displayed in the orchard skin.
/// </Summary>
[Themed]
Public Class Homecontroller: Controller
{
/// <Summary>
/// Corresponding to the index action that is executed by default in the route
/// </Summary>
/// <Returns> </returns>
Public Actionresult index ()
{
String Model = " Hello world! " ; // This is the model, which is directly written in the code. Of course, it can be retrieved from the database
Return View (( Object ) Model ); // By default, indexes in the Views \ home directory are used
}
}
Create a view and create an index. cshtml file (corresponding to action) in the Views directory of the module ). Enter the following code: Index. cshtml
<! -- Present the data transmitted by the Controller -->
< H2 > @ This. Model </ H2
Note: The engine used in orchard is razor.
To enable the module, enter the following command in the orchard command line to start the module: Feature enable mycompany. helloworld Of course, you can also log on to the background and enable it in:/admin/modules/Features
Use the module to directly enter the orchard website address/helloworld in the browser, such as http: // localhost/helloworld, to access the module we created.
Conclusion In this article, I have created a very simple module. It processes A/helloworld URL request through the index action in homecontroller and displays Hello world! . The functions of the module are simple, but they also cover the basic operations of creating a module to enable the module and the basic concept of running the module as an MVC area. It laid the foundation for the subsequent in-depth study of orchard module development.
Download this example: click here
Reference:
Example: Drawing (V = vs.98). aspxrazor view engine Introduction: http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx