29.05.2015 Views

o_19mgorv9t13a3ko71fev19l81mqa.pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

[ActionName("Enumerate")]<br />

public ViewResult List() {<br />

return View("Result", new Result {<br />

ControllerName = "Customer",<br />

ActionName = "List"<br />

});<br />

}<br />

}<br />

}<br />

[NonAction]<br />

public ActionResult MyAction() {<br />

return View();<br />

}<br />

The MyAction method in the listing will not be considered as an action method, even though it meets all of the criteria that<br />

the invoker looks for. This is useful for ensuring that you do not expose the workings of your controller classes as actions. Of<br />

course, normally such methods should simply be marked private, which will prevent them from being invoked as actions;<br />

however, [NonAction] is useful if for some reason you must mark such a method as public. Requests for URLs that<br />

target NonAction methods will generate 404—Not Found errors, as shown in Figure 19-7.<br />

Figure 19-7. The effect of requesting a URL that targets a NonAction method<br />

Creating a Custom Action Method Selector<br />

Action method selectors are derived from the ActionMethodSelectorAttribute class, which is shown in Listing<br />

19-18.<br />

Listing 19-18. The ActionMethodSelectorAttribute Class<br />

using System.Reflection;<br />

namespace System.Web.Mvc {<br />

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false,<br />

518

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!