具有属性路由的多个GET不好?

编程入门 行业动态 更新时间:2024-10-12 22:29:41
本文介绍了具有属性路由的多个GET不好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这应该是对的:

/api/MyDataController.cs

/api/MyDataController.cs

public class MyDataController: ApiController { [HttpGet] [Route("GetOne")] public IHttpActionResult GetOne() { } // works w/o GetTwo [HttpGet] [Route("GetTwo")] public IHttpActionResult GetTwo() { } }

.js

$http({method: 'GET', url: '/api/MyData/GetOne'})... //works w/o GetTwo $http({method: 'GET', url: '/api/MyData/GetTwo'})...

与这篇文章相同, API版本为

Same as this post, API version is

<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net461" />

呼叫一个和两个都抱怨 GetOne ,

找到了多个与请求匹配的动作:类型为GetOne MyWeb.API.MyDataControllerGetOne的类型 MyWeb.API.MyDataController"

"Multiple actions were found that match the request: GetOne on type MyWeb.API.MyDataControllerGetOne on type MyWeb.API.MyDataController"

如果从Api控制器中重新删除GetTwo()则可以使用.

It works if rem-out GetTwo() from Api controller.

推荐答案

这似乎是应用程序仍在使用基于约定的路由.

This looks like the application is still using convention-based routing.

发生冲突的原因是因为默认的基于约定的路由模板api/{controller}/{id}通常不提供像api/{controller}/{action}/{id}这样的操作参数.如果您希望在使用之前提供的模板时通过常规路由使发布操作起作用.

The reason for the clash is because the default convention-based route template api/{controller}/{id}does not usually provide an action parameter like this api/{controller}/{action}/{id}. If you want to get post actions to work via convention-routing when use the template provided before.

如果要使用属性路由,则需要在WebApiConfig.cs文件中启用属性路由,以允许Rout Atrribute工作.

If you want to use attribute routing instead then you need to enable attribute routing in the WebApiConfig.cs file in order to allow the Rout Atrribute to work.

public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Attribute routing. config.MapHttpAttributeRoutes(); // Convention-based routing. config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } }

您还需要更新路线以获取想要的东西

You would also need to update your routes to get what you want

[RoutePrefix("MyData")] public class MyDataController: ApiController { //GET MyData/GetOne [HttpGet] [Route("GetOne")] public IHttpActionResult GetOne() { } //GET MyData/GetTwo [HttpGet] [Route("GetTwo")] public IHttpActionResult GetTwo() { } }

此处的属性路由阅读 ASP.NET Web API 2中的属性路由

更多推荐

具有属性路由的多个GET不好?

本文发布于:2023-11-25 23:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631739.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   路由   属性   不好

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!