Part 68 - What is the use of NonAction attribute in mvc
来源:互联网 发布:武清龙济 知乎 编辑:程序博客网 时间:2024/05/21 07:07
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.
- Part 68 - What is the use of NonAction attribute in mvc
- What is the use of "?
- what is the use of truncate command
- What is the use of extern in C(C语言中extern的用途)?
- What is the use of having destructor as private?
- 【VB/.NET】What Is Attribute In .NET?
- what is the mean of "GPRINT Type" in cacti
- What is the significance of asmlinkage modifier in C?
- what is the purpose of using translatable in Android strings?
- What Is The Meaning of 0.0.0.0 In Routing Table?
- What is the advantage of load() vs get() in Hibernate?
- Part 71 - childactiononly attribute in mvc
- Part 72 - HandleError attribute in mvc
- What is the use of the res/values/public.xml file on Android?
- what is app.use in NodeJS/Express
- What is the general use of Awake(), Start(), Update(), FixedUpdate(), LateUpdate()?
- To see what is in the MAT-file prior to loading it, use whos -file
- White space is required before the attribute type in the declaration of attribute "t" for element "b
- 【Hibernate】OpenSession和getCurrentSession的区别
- Part 69 - Action filters in mvc
- linux驱动开发:串口协议
- BBR: Bottleneck Bandwith and RTT Congestion Control Algorith For TCP
- oj第十一周训练——查闰年
- Part 68 - What is the use of NonAction attribute in mvc
- JAVA遍历Map集合的方法
- Ajax在JQuery中的应用(Get方法练习2)
- 渗透————sqlmap
- 解决oracle sqlplus 中上下左右backspace不能用
- AsyncTask源码分析
- Filter过滤器
- 454. 4Sum II
- Part 70 - Authorize and AllowAnonymous action filters in mvc