ASP.Net MVC选择器

动作选择器是可以应用于动作方法的属性,用于影响响应请求时调用哪个动作方法。 它有助于路由引擎选择正确的操作方法来处理特定的请求。

当你编写动作方法时,它扮演着非常关键的角色。 这些选择器将根据动作方法前面给出的修改后的名称来决定方法调用的行为。动作选择器通常用于别名操作方法的名称。

动作选择器有三种类型的属性 -

  • ActionName
  • NonAction
  • ActionVerbs

1. ActionName

这个类表示一个用于动作名称的属性。它还允许开发人员使用与方法名称不同的动作名称(即动作的别名)。

我们来创建一个项目: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")]来应用GetCurrentTimeActionName选择器,如以下代码所示 -

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

2. NonAction

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-找不到” 的错误。

3. ActionVerbs

另一个可以应用的选择器过滤器是ActionVerbs属性。所以这限制了特定行为对特定的HttpVerbs的指示。可以定义两个具有相同名称的不同操作方法,但是一个操作方法会响应HTTP Get请求,另一个操作方法会响应HTTP Post请求。

MVC框架支持以下ActionVerbs -

  • HttpGet
  • HttpPost
  • HttpPut
  • HttpDelete
  • HttpOptions
  • HttpPatch

下面来看一个简单的例子,创建一个控制器 - 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框架无法确定应该为请求选择哪个操作方法。

使用下面的代码来指定HttpGet ActionVerb作为响应的动作。

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);
        }
    }
}

当运行此应用程序时,将收到以下输出结果 -


上一篇: ASP.Net MVC过滤器 下一篇: ASP.Net MVC视图