【问题标题】:MVC 6 Multiple Get MethodsMVC 6 多种获取方法
【发布时间】:2016-01-18 01:58:26
【问题描述】:

我试图支持每个控制器的多个 Get() 方法,以及通过 web api 访问的特殊命名的方法。我已经在 MVC 5 中做到了这一点,但似乎无法弄清楚它是如何在 MVC 6 中完成的。有什么想法吗?谢谢。

【问题讨论】:

    标签: c# asp.net-core-mvc asp.net-core-routing


    【解决方案1】:

    您不能有多个具有相同 url 模式的 Get 方法。您可以使用属性路由并为不同的 url 模式设置多个 GET 方法。

    [Route("api/[controller]")]
    public class IssuesController : Controller
    {
        // GET: api/Issues
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "item 1", "item 2" };
        }
    
        // GET api/Issues/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "request for "+ id;
        }
    
        // GET api/Issues/special/5
        [HttpGet("special/{id}")]
        public string GetSpecial(int id)
        {
            return "special request for "+id;
        }
        // GET another/5
        [HttpGet("~/another/{id}")]
        public string AnotherOne(int id)
        {
            return "request for AnotherOne method with id:" + id;
        }
        // GET api/special2/5
        [HttpGet()]
        [Route("~/api/special2/{id}")]
        public string GetSpecial2(int id)
        {
            return "request for GetSpecial2 method with id:" + id;
        }
    }
    

    您可以看到我同时使用了HttpGet 和Route 属性来定义路由模式。

    通过以上配置,您将得到以下响应

    请求网址:yourSite/api/issues/

    结果["value1","value2"]

    请求网址:yourSite/api/issues/4

    结果request for 4

    请求网址:yourSite/api/special2/6

    结果request for GetSpecial2 method with id:6

    请求网址:yourSite/another/3

    结果request for AnotherOne method with id:3

    【讨论】:

    • 太棒了!非常感谢。
    • 请看我的question
    【解决方案2】:

    你可以使用属性路由链接这个-

    [Route("api/[controller]")] /* this is the defualt prefix for all routes, see line 20 for overridding it */
    public class ValuesController : Controller
    {
        [HttpGet] // this api/Values
        public string Get()
        {
            return string.Format("Get: simple get");
        }
    
        [Route("GetByAdminId")] /* this route becomes api/[controller]/GetByAdminId */
        public string GetByAdminId([FromQuery] int adminId)
        {
            return $"GetByAdminId: You passed in {adminId}";
        }
    
        [Route("/someotherapi/[controller]/GetByMemberId")] /* note the / at the start, you need this to override the route at the controller level */
        public string GetByMemberId([FromQuery] int memberId)
        {
            return $"GetByMemberId: You passed in {memberId}";
        }
    
        [HttpGet]
        [Route("IsFirstNumberBigger")] /* this route becomes api/[controller]/IsFirstNumberBigger */
        public string IsFirstNumberBigger([FromQuery] int firstNum, int secondNum)
        {
            if (firstNum > secondNum)
            {
                return $"{firstNum} is bigger than {secondNum}";
            }
            return $"{firstNum} is NOT bigger than {secondNum}";
        }
    }
    

    查看这里了解更多详情 - http://nodogmablog.bryanhogan.net/2016/01/asp-net-5-web-api-controller-with-multiple-get-methods/

    【讨论】:

    • 谢谢!这是一个更好的解决方案,因为我可以使用普通的嵌套路由。
    • 请看我的question
    猜你喜欢
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 2013-12-15
    • 2018-04-08
    相关资源
    最近更新 更多