动作选择器是可以应用于动作方法的属性,用于影响响应请求时调用哪个动作方法。 它有助于路由引擎选择正确的操作方法来处理特定的请求。
当你编写动作方法时,它扮演着非常关键的角色。 这些选择器将根据动作方法前面给出的修改后的名称来决定方法调用的行为。动作选择器通常用于别名操作方法的名称。
动作选择器有三种类型的属性 -
这个类表示一个用于动作名称的属性。它还允许开发人员使用与方法名称不同的动作名称(即动作的别名)。
我们来创建一个项目:mvcselectors,在这个项目中,创建一个控制器 - homecontroller ,它包含有两个操作方法。参考以下代码 -
using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; namespace mvcselectors.controllers { public class homecontroller : controller { // get: home public string index() { return "this is asp.net mvc filters tutorial"; } public string getcurrenttime() { return datetime.now.tostring("yyyy-mm-dd hh:mm:ss"); } } }
通过在getcurrenttime()方法之上写[actionname("currenttime")]来应用getcurrenttime的actionname选择器,如以下代码所示 -
using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; namespace mvcselectors.controllers { public class homecontroller : controller { // get: home public string index() { return "this is asp.net mvc filters tutorial"; } [actionname("currenttime")] public string getcurrenttime() { return datetime.now.tostring("yyyy-mm-dd hh:mm:ss"); } } }
现在运行该应用程序并在浏览器url栏中输入以下url:http://localhost:62833/home/currenttime(不是getcurrenttime),将收到以下输出结果 -
可以看到已经使用了currenttime,而不是原来的动作名称,在上面正常的url应该是:getcurrenttime。
nonaction是另一个内置属性,它表示controller的公共方法不是一个操作方法。当想要一个方法不要被当作一个操作方法来处理的时候,就可以使用它了。
下面来看看一个简单的例子,在homecontroller中添加另一个方法,并使用下面的代码应用nonaction属性。
using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; namespace mvcselectors.controllers { public class homecontroller : controller { // get: home public string index() { return "this is asp.net mvc filters tutorial"; } [actionname("currenttime")] public string getcurrenttime() { return timestring(); } [nonaction] public string timestring() { return "当前时间是: " + datetime.now.tostring("t"); } } }
新的方法timestring()是要被getcurrenttime()方法调用的,但不能在url中作为动作使用。
运行这个应用程序,并在浏览器中指定以下url:http://localhost:62466/home/currenttime, 将收到以下输出。如下图所示 -
现在来看看在url中使用/home/timestring作为动作,看看会发生什么。如下图所示 -
可以看到它给出了“404-找不到” 的错误。
另一个可以应用的选择器过滤器是actionverbs属性。所以这限制了特定行为对特定的httpverbs的指示。可以定义两个具有相同名称的不同操作方法,但是一个操作方法会响应http get请求,另一个操作方法会响应http post请求。
mvc框架支持以下actionverbs-
下面来看一个简单的例子,创建一个控制器 -employeecontroller。
using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; namespace mvcselectors.controllers { public class employeecontroller : controller { // get: employee public actionresult search(string name = "no name entered") { var input = server.htmlencode(name); return content(input); } } }
现在使用下面的代码添加另一个具有相同名称的操作方法。
using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; namespace mvcselectors.controllers { public class employeecontroller : controller { public actionresult search(string name) { var input = server.htmlencode(name); return content(input); } public actionresult search() { var input = "another search action"; return content(input); } } }
当运行这个应用程序时,它会给出一个错误,因为mvc框架无法确定应该为请求选择哪个操作方法。
使用下面的代码来指定httpgetactionverb作为响应的动作。
using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; namespace mvcselectors.controllers { public class employeecontroller : controller { public actionresult search(string name) { var input = server.htmlencode(name); return content(input); } [httpget] public actionresult search() { var input = "another search action"; return content(input); } } }
当运行此应用程序时,将收到以下输出结果 -