MVC .Net Method Glitch或Gloat

时间:2016-06-03 18:07:06

标签: c# asp.net-mvc asp.net-mvc-4

如下所示的控制器可以在没有任何方法前缀(get,post,..)的情况下使用

    public ActionResult SearchNearAddress(string Longitude, string Latitude, string Address )
    {
        return View();
    }

这有点好,因为它允许我为所有目的使用相同的控制器(获取和发布)并节省代码。

这是一个糟糕的设计,还是一个不错的功能?!

1 个答案:

答案 0 :(得分:3)

如果您按照以下方式指定行动:

[HttpGet]
public ActionResult SearchNearAddress(string Longitude, string Latitude, string Address )
{
     return View();
}

然后你只允许GET发生。这称为动作选择器属性。其他方法还有其他方法(PUT,POST等)。

通过定义没有它们的操作,您可以指定您的操作应该响应任何方法。