Part 68 - What is the use of NonAction attribute in mvc
2016-11-13 13:14
567 查看
The following questions could be asked in an interview
What is the use of NonAction attribute in MVC?
OR
How do you restrict access to public methods in a controller?
An action method is a public method in a controller that can be invoked using a URL. So,
by default, if you have any public method in a controller then it can be invoked using a URL request. To restrict access to public methods in a controller, NonAction attribute can
be used. Let's understand this with an example.
We have 2 public methods in HomeController, Method1() and Method2().
Method1 can be invoked using URL /Home/Method1
Method2 can be invoked using URL /Home/Method2
public class HomeController : Controller
{
public string Method1()
{
return "<h1>Method 1 Invoked</h1>";
}
public string Method2()
{
return "<h1>Method 2 Invoked</h1>";
}
}
Let's say Method2() is for doing some internal work, and we don't want it to be invoked using a URL request. To achieve this, decorate Method2()
with NonAction attribute.
[NonAction]
public string Method2()
{
return "<h1>Method 2 Invoked</h1>";
}
Now, if you naviage to URL /Home/Method2, you will get an error - The resource cannot be found.
Another way to restrict access to methods in a controller, is by making them private.
private string Method2()
{
return "<h1>Method 2 Invoked</h1>";
}
In general, it's a bad design to have a public method in a controller that is not an action method. If you have any such method for performing business
calculations, it should be somewhere in the model and not in the controller.
However, if for some reason, if you want to have public methods in a controller and you don't want to treat them as actions, then use NonAction
attribute.
What is the use of NonAction attribute in MVC?
OR
How do you restrict access to public methods in a controller?
An action method is a public method in a controller that can be invoked using a URL. So,
by default, if you have any public method in a controller then it can be invoked using a URL request. To restrict access to public methods in a controller, NonAction attribute can
be used. Let's understand this with an example.
We have 2 public methods in HomeController, Method1() and Method2().
Method1 can be invoked using URL /Home/Method1
Method2 can be invoked using URL /Home/Method2
public class HomeController : Controller
{
public string Method1()
{
return "<h1>Method 1 Invoked</h1>";
}
public string Method2()
{
return "<h1>Method 2 Invoked</h1>";
}
}
Let's say Method2() is for doing some internal work, and we don't want it to be invoked using a URL request. To achieve this, decorate Method2()
with NonAction attribute.
[NonAction]
public string Method2()
{
return "<h1>Method 2 Invoked</h1>";
}
Now, if you naviage to URL /Home/Method2, you will get an error - The resource cannot be found.
Another way to restrict access to methods in a controller, is by making them private.
private string Method2()
{
return "<h1>Method 2 Invoked</h1>";
}
In general, it's a bad design to have a public method in a controller that is not an action method. If you have any such method for performing business
calculations, it should be somewhere in the model and not in the controller.
However, if for some reason, if you want to have public methods in a controller and you don't want to treat them as actions, then use NonAction
attribute.
相关文章推荐
- What is the use of extern in C(C语言中extern的用途)?
- What is the use of "?
- 软件随想录(local.joelonsoftware.com/wiki)-2001年05月05日 这个国家的狗做什么工作? - What Is the Work of Dogs in this Cou
- InnoDB: auto-extending data file ./ibdata1 is of a different size 640 pages (rounded down to MB) than specified in the .cnf file: initial 768 pages, max 0 (relevant if non-zero) pages!
- what is the use of truncate command
- What is the difference between SVD and matrix factorization in context of recommendation engine?
- What is the significance of asmlinkage modifier in C?
- What is the general use of Awake(), Start(), Update(), FixedUpdate(), LateUpdate()?
- 本地提交SVN报错 提交文件 is not known to exist in the repository and is not part of the commit, yet its child
- ccah-500 第47题 What is the purpose of ZooKeeper in such a configuration
- Question 38: What is the output of the program below in C++?
- Question 10: Given the following program snippet, what can we conclude about the use of dynamic_cast in C++?
- What is the purpose of @SmallTest, @MediumTest, and @LargeTest annotations in Android?
- .NET错误The 'targetFramework' attribute in the <compilation> element of the Web.config file is used only to target version 4.0 and later of the .NET Framework
- what is the difference of select single and select up to one row in abap
- HOW TO: Change the Owner of a User-Defined Data Type That Is in Use in SQL Server 2000
- Is it possible to use a checkbox in a split button list instead of the gear icon?
- What is the maximum number of files allowed in a HDFS directory?
- What is the advantage of load() vs get() in Hibernate?
- What Is The Meaning of 0.0.0.0 In Routing Table?