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);
}