asp.net mvc tricks 1-2

Source: Internet
Author: User
Tags abstract filter html page net return string
See Anytao and Terrylee share of the asp.net MVC use and optimization techniques, unavoidably preface, also share this more than a year to asp.net the accumulation of MVC development.

It may not be a number of efficient skills, but it does solve the problem, it may not have any advanced principles, but I think it is worth sharing.

1.Controller to control the title of HTML

I think most friends have the need to specify the HTML page title in controller.

I am accustomed to writing the code before perfecting its implementation, and specifying a title is the most convenient form:

1:public actionresult Index (int id) {

2:var article=db.getarticle (ID);//Get the article in the database

3:title=article. Name;

4:return View ();

5:}

Of course, this code is not executable, because Controller does not have the built-in title attribute, but it doesn't matter, we can customize one:

1:abstract public class Mybasecontroller:controller {

2:public string Title {

3:set {

4:viewdata["page_title"] = value;

5:}

6:}

7:}


Then change our controller to this mybasecontroller, then write the output of viewdata["Page_title" in master.


1: <title><%=viewdata["Page_title"]%></title>

OK, this is a wish to achieve.

Of course, do SEO words keyword and description can also do so.
The context defined in controller is passed in 2.ViewModel

Lao Zhao very push ViewModel so I also did a lot of practice in this respect, found that is really good. But one question is how the context generated in the controller can be uploaded to the view, such as custom user information, and some types that are not static, and I don't want to instantiate it again in view.

Workaround: Add a Context attribute in ViewModel (these contexts in me and in the program inherit from the Icontext interface, and in controller its properties are chcontext)


1:public class Homeindexviewmodel {

2:public Icontext Context {get; set;} This property is the way to solve it.

3:public string Message {get; set;}

4:}


And I'm in the controller:

1:public ActionResult Index () {

2:homeindexviewmodel model = new Homeindexviewmodel {

3:context = chcontext,//Here Pass

4:message= "Welcome to asp.net mvc!"

5:};

6:return View (model);

7:}


And the View:


1: <%@ Page language= "C #" masterpagefile= "~/views/shared/site.master"

2:inherits= "System.web.mvc.viewpage

3: <asp:content id= "Indextitle" contentplaceholderid= "titlecontent" runat= "Server" >

4:home Page

5: </asp:Content>

6: <asp:content id= "indexcontent" contentplaceholderid= "maincontent" runat= "Server" >

7:

8: </asp:Content>

This enables us to deliver the custom context that is generated in the controller. But each ViewModel initializes a icontext, which is too frequent and tiring, and then further improves, we use the filter that acts on controller, we customize the base class in controller Mybasecontroller The following filter is written, and the implementation of this function is to have all ViewModel inherited from a class: Mybaseviewmodel:

Mybaseviewmodel and Model:


1:public class Mybaseviewmodel {

2:public Icontext Context {get; set;}

3:}

4:

5:public class Homeindexviewmodel:mybaseviewmodel {

6:public string Message {get; set;}

7:}

Controller and Filter

1:abstract public class Basecontroller:controller {

2:protected override void Onresultexecuting (ResultExecutingContext filtercontext) {

3:var m = Viewdata.model as Baseviewmodel;

4:if (M!= null) {

5:m.context = chcontext;//is initialized here

6:}

7:}

8:}


This time we controller in the use of the refreshing, no longer pass the Chcontext



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.