Action requirement and view essence

Source: Internet
Author: User
Tags abstract

Create a requirement that the custom action must meet

The method must be public.

Method cannot be a static method.

Method cannot be a method in the control base class (e.g., Tostring,gethashcode, etc.)

Method cannot be an extension method.

Method cannot be a constructor, getter, setter.

Method cannot contain ref or out parameters.

Using the Nonactionattribute attribute will prevent the action from being invoked

Namespace MVCViewAndAction.Web.Controllers
{
    [handleerror] public
    class Userdemocontroller:controller {////Get
        :/userdemo///
        Custom A simple method
        [nonaction]  
        //public sealed class Nonactionattribute represents an attribute that is used to indicate that the Controller method is not an operator method

. Public
        string displaystring ()
        {return
            ' is a demo string! ';
        }
}}

Understanding views

Compared to asp.net and Active Server pages, asp.net MVC does not contain any directly corresponding pages. In the ASP.net MVC application, you type the URL in the browser's address bar without a corresponding page on the disk, which is mapped to the controller actions. The closest thing to page pages is what we call view.

The most basic is as follows:

Public ActionResult Index ()
{return
     View ();
}

To explore the nature of view to show the following results:

Code highlighting produced by Actipro 

Codehighlighter (freeware)
http://www. codehighlighter.com/
    
--> public class Userdemocontroller:controller
    {public
        rssactionresult rssshow ()
        {return
            new Rssactionresult ();
        }
    }

We need to create a Rssactionresult class that inherits ActionResult, as follows:

Code 

highlighting produced by Actipro Codehighlighter (freeware)
http://www. codehighlighter.com/
    
-->namespace mvcviewandaction.common
{
     * * SYSTEM.WEB.MVC
    Public Abstract class ActionResult
    {
        protected actionresult ();
        public abstract void Executeresult (ControllerContext context);
    } *
    /public class Rssactionresult:actionresult
    {public
        rssactionresult ()
        { 
        }
        // Public
        override void Executeresult (ControllerContext context)
        {
            if (context = = null)
            {
                throw new ArgumentNullException ("ControllerContext is null!");
            }
            Httpresponsebase response = context. Httpcontext.response;
            RSS RSS = new RSS ();
            Rss. Createsamplerss (response);
        }
    

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.