ASP.NET MVC NonAction meaning
Can anybody please tell me why should I use the NonAction
attribute? I mean say I have a form with several subm开发者_运维知识库it values: Update, Delete or Insert. Since all the submit buttons have the same form in common I'm switching the submit value inside the controller and act accordingly.
Like this:
public ActionResult asd(string submitButton){
switch(submitButton){
case "Insert":
return Insert();
// bla bla bla
}
}
[NonAction]
public ActionResult Insert(){
// some code inside here
return View();
}
Once again, why should I use NonAction instead of something like this:
public void Insert(){
// some code inside here
}
You can omit the NonAction
attribute but then the method is still invokable as action method.
From the MSDN site (ref):
By default, the MVC framework treats all public methods of a controller class as action methods. If your controller class contains a public method and you do not want it to be an action method, you must mark that method with the NonActionAttribute attribute.
It is worth noting that the need to use [NonAction]
applies only to public methods. Protected and private methods are not treated as actions. Since your Update
/Delete
/Insert
methods are helpers for asd()
, a private method would be more appropriate for your scenario:
public ActionResult asd(string submitButton){
switch(submitButton){
case "Insert":
return Insert();
// bla bla bla
}
}
ActionResult Insert(){
// some code inside here
}
Reading Haack's Article
Any public method in a controller class is callable via URL.
Sometimes you may need to avoid this. For example, if you implement some interface and you may not want to call that public method you can mark as NonAction
public interface IEmployee
{
void Save(Employee e);
bool Validate(Employee e);
}
public class EmployeeController:Controller, IEmployee
{
public void Save(Employee e){
}
[NonAction]
public void Validate(Employee e){
}
}
I just used [NonAction] in our web api, to decorate a bunch of Controller Methods (endpoints) because we had a last minute decision that we will postpone the delivery of the specific endpoints.
So it is useful, if you want to avoid exposing an API endpoint, but still want to keep the implementation for later.
So I used this attribute and it saved me a lot of time.
I will just remove it in the next release and this will simply be there!
If you don't use the [NonAction] attribute then someone can call your action directly instead of having to go through the 'asd' function
NonAction attribute makes an action non accessible from the navigation bar. For example if you have an action which deletes items in database, you have to add the NonAction
attribute to make it not accessible by users.
Firstly, think of an ActionResult simply as a particular type of construct that is returned by MVC, that happens to provide a particular convenience in terms of the way that ActionResult can be internally handled within the MVC framework. So the fact that something is an ActionResult doesn't necessarily mean "this should be publicly available". In fact, any public method in an MVC controller will be treated as an action method, whether or not it returns an ActionResult.
So, simply having a return type that isn't an ActionResult will not necessarily prevent that method from being exposed as a publicly available action that can be called via a URL.
There may be many reasons you don't want to expose a method as an action that can be invoked via a url, and in the case that you want to 'protect' against this, that's when you use the [NonAction'] attribute.
This is indicate that a controller method is not an action method.Example: [NonAction] public void IndexTest() { // Do some thing } this is very useful attribute when visibility of controller 's method cannot be changed to private.
If you did not want to invoke some action methods then you have to mark it with a attribute [NonAction]
or by making it private
public ActionResult Index(){
return View();
}
[NonAction]
public ActionResult Countries(List<string>countries){
return View(countries);
}
you can copy the code and paste it and see the result.thanks
精彩评论