当ActionInvoker选取Controller中的Action时,会默认应用反射机制找到相同名字的方法,这个过程就是动作名称选择器运作的过程,这个选择查找过程对Action的名称字符大小写不进行区分。

1.ActionName特性

       如果在Action加上ActionName特性,此时路由参数action的值就会改变。

public class HomeController: Controller
{
    [ActionName("Default")]
    public ActionResult Index()
    {
        return View();
    }
} 

      此时访问Index动作,需要使用路由http://localhost/Home/Default,并且ASP.NET MVC会去寻找/Views/Home/Default.cshtml视图页面来运行,而不是/Views/Home/Index.cshtml。

      需要注意的是,应该避免多个方法对应同一个Action名称,此错误不会再编译时被发现,仅在运行时请求对应Action才引发异常。

相关文章:

  • 2022-03-04
  • 2021-12-08
  • 2021-09-21
  • 2021-12-08
  • 2021-11-27
  • 2021-06-24
  • 2021-06-04
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-28
  • 2021-12-10
  • 2021-09-12
  • 2022-01-01
  • 2021-07-12
相关资源
相似解决方案