Go ASP. NET MVC Widget Development-Viewengine

Source: Internet
Author: User

This article transferred from: http://www.cnblogs.com/hsinlu/archive/2011/03/02/1968796.html

In the ASP. NET Development Blog class system, we often use widgets, such as online friends, recently visited friends, the latest message, and so on, about the difference between ASP. NET MVC and ASP. List to introduce the implementation of this functionality and the structural design in ASP.

    • Development tools: VS. EN
    • Development language: Visual C #
    • ASP. NET MVC 3
    1. ASP. NET MVC Widget-Design
    2. ASP. NET MVC Widget-controller Controller
    3. ASP. NET MVC Widget-viewengine
    4. ASP. NET MVC widget-mobile support
    5. ASP. NET MVC Widget-html.widget extension method

About Viewengine This is the core of the widgets implementation, where you need to customize the view engine for ASP. NET MVC, that is, to have the ASP. NET MVC view engine find files in widgets.

Direct access the following error occurred, indicating that the display file could not be found

The directory where the ASP. NET MVC default engine searches for files is also visible by error

/views/widget/

/views/shared/

To find files in "widgets" you have to have the view engine search for files in "widgets"

such as Friends:/widgets/friends/

Adding a custom View engine

1. Add "WidgetViewEngine.cs", Inherit from Buildmanagerviewengine

UsingSystem;UsingSystem.Collections.Generic;UsingSystem.Linq;Usingsystem.web;UsingSYSTEM.WEB.MVC;
NamespaceWidgets {Public ClassWidgetviewengine:buildmanagerviewengine {Internal Static ReadOnly StringViewstartfilename= "_viewstart";
PublicWidgetviewengine ():This(Null) { }
PublicWidgetviewengine (Iviewpageactivator viewpageactivator):Base(Viewpageactivator) {Areaviewlocationformats= New[] {"~/areas/{2}/views/{1}/{0}.cshtml","~/areas/{2}/views/shared/{0}.cshtml"}; Areamasterlocationformats= New[] {"~/areas/{2}/views/{1}/{0}.cshtml","~/areas/{2}/views/shared/{0}.cshtml"}; Areapartialviewlocationformats= New[] {"~/areas/{2}/views/{1}/{0}.cshtml","~/areas/{2}/views/shared/{0}.cshtml"};
Viewlocationformats= New[] {"~/views/{1}/{0}.cshtml","~/views/shared/{0}.cshtml"
}; Masterlocationformats= New[] {"~/views/{1}/{0}.cshtml","~/views/shared/{0}.cshtml"}; Partialviewlocationformats= New[] {"~/{1}s/{0}/widget.cshtml","~/views/{1}/{0}.cshtml","~/views/shared/{0}.cshtml"};
Fileextensions= New[] {"cshtml"}; }
Protected OverrideIView Createpartialview (ControllerContext controllercontext,StringPartialpath) {Return NewRazorview (ControllerContext, Partialpath, Layoutpath:Null, Runviewstartpages:False, Viewstartfileextensions:fileextensions, Viewpageactivator:viewpageactivator); }
protected override IView CreateView (controllercontext controllercontext, string ViewPath, string masterpath) {var view = new Razorview (ControllerContext, ViewPath, Layoutpath: Masterpath, Runviewstartpages: true, Viewstartfileextensions:fileextensions, Viewpageactivator: Viewpageactivator); return View;}} }

2. Add Appstart_widgets.cs, modify the default view engine

UsingSystem;UsingSystem.Collections.Generic;UsingSystem.Linq;Usingsystem.web;UsingSYSTEM.WEB.MVC;UsingWidgets;
[Assembly:preapplicationstartmethod (typeof(Appstart_widgets),"start)]
namespace Widgets {public class Appstart_ Widgets {public static void Start () {// Clear system default view Engines ViewEngines.Engines.Clear (); VIEWENGINES.ENGINES.ADD (new Widgetviewengine ())}}}

3. Add friend entity and test data

UsingSystem;UsingSystem.Collections.Generic;UsingSystem.Linq;Usingsystem.web;
NamespaceWidgets.models {Public ClassFriend {Public IntId {Get;Set; }Public StringName {Get;Set; } }
Public ClassFriendscontext {PublicFriendscontext () {Friends= NewList<Friend>() {NewFriend{id=1, Name="Zhang San"},NewFriend{id=2, Name="Li Si "" new Friend{id=3,name< Span style= "color: #000000;" >= "wang wu" } };
public IEnumerable <friend> Friends {getprivate set; } } }

4. Change widgets/friends/widget.cshtml

@model IEnumerable<Widgets. Models.friend>@{Layout = "~/widgets/_layout.cshtml"; Viewbag.title = "My friends"; }<Divclass= "newsletter" > <ul> @foreach (var item in Model) {<li> @item. Name</li>} </ul> </div>

Project structure

Other codes are detailed in the source code.

When you visit again, you will see the result.

SOURCE download

Go ASP. NET MVC Widget Development-Viewengine

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.