我开始使用MVC4 Web API项目,我有多种HttpPost方法的控制器.控制器如下所示:
调节器
public class VTRoutingController : ApiController
{
[HttpPost]
public MyResult Route(MyRequestTemplate routingRequestTemplate)
{
return null;
}
[HttpPost]
public MyResult TSPRoute(MyRequestTemplate routingRequestTemplate)
{
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
这里MyRequestTemplate表示负责处理通过请求的Json的模板类.
错误:
当我让使用招为请求http://localhost:52370/api/VTRouting/TSPRoute或http://localhost:52370/api/VTRouting/Route 我得到一个错误:
找到了与请求匹配的多个操作
如果我删除上述方法之一,它工作正常.
Global.asax中
我已经尝试修改默认路由表global.asax,但我仍然收到错误,我认为我在global.asax中定义路由时遇到问题.这是我在global.asax中所做的.
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapHttpRoute(
name: "MyTSPRoute",
routeTemplate: "api/VTRouting/TSPRoute",
defaults: new { }
);
routes.MapHttpRoute(
name: "MyRoute",
routeTemplate: "api/VTRouting/Route",
defaults: new { action="Route" }
);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用POST在Fiddler中发出请求,在RequestBody中为MyRequestTemplate传递json.
我正在使用Web API,我是新手.我陷入了路由问题.我有一个控制器,有以下动作:
// GET api/Ceremony
public IEnumerable<Ceremony> GetCeremonies()
{
return db.Ceremonies.AsEnumerable();
}
// GET api/Ceremony/5
public Ceremony GetCeremony(int id)
{
Ceremony ceremony = db.Ceremonies.Find(id);
return ceremony;
}
public IEnumerable<Ceremony> GetFilteredCeremonies(Search filter)
{
return filter.Ceremonies();
}
Run Code Online (Sandbox Code Playgroud)
将操作添加GetFilteredCeremonies到控制器时出现问题.添加此项后,当我进行ajax调用GetCeremonies操作时,它会返回一个Exception并显示以下消息:
"Message":"An error has occurred.","ExceptionMessage":"Multiple actions were
found that match the request
Run Code Online (Sandbox Code Playgroud)
仅供参考:参数Search是Model类,它包含属性和函数名称Ceremonies.
编辑
路线:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud) 我有一个Web API控制器,其中包含以下操作:
[HttpPut]
public string Put(int id, JObject data)
[HttpPut, ActionName("Lock")]
public bool Lock(int id)
[HttpPut, ActionName("Unlock")]
public bool Unlock(int id)
Run Code Online (Sandbox Code Playgroud)
并映射了以下路线:
routes.MapHttpRoute(
name: "Api",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapHttpRoute(
name: "ApiAction",
routeTemplate: "api/{controller}/{action}/{id}"
);
Run Code Online (Sandbox Code Playgroud)
当我提出以下请求时,一切都按预期工作:
PUT /api/items/Lock/5
PUT /api/items/Unlock/5
Run Code Online (Sandbox Code Playgroud)
但是当我试图提出要求时:
PUT /api/items/5
Run Code Online (Sandbox Code Playgroud)
我得到以下异常:
Multiple actions were found that match the request:
Put(int id, JObject data)
Lock(int id)
Unlock(int id)
Run Code Online (Sandbox Code Playgroud)
我尝试在默认路由中添加一个空操作名称,但这没有帮助:
[HttpPut, ActionName("")]
public string Put(int id, JObject data)
Run Code Online (Sandbox Code Playgroud)
有关如何将默认RESTful路由与自定义操作名称相结合的任何想法?
编辑:路由机制不会被控制器的选择混淆.它被单个控制器上的动作选择所迷惑.我需要的是在没有指定动作时匹配默认动作.希望澄清事情.
我得到了这条路线:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
这个动作:
[System.Web.Http.HttpPost]
[System.Web.Http.ActionName("GetLoginSeed")]
public object GetLoginSeed()
[System.Web.Http.HttpPost]
[System.Web.Http.AllowAnonymous]
[System.Web.Http.ActionName("Authenticate")]
public object PerformLogin(JObject jr)
Run Code Online (Sandbox Code Playgroud)
这是帖子请求:
http://localhost:61971/api/Login/GetLoginSeed
Run Code Online (Sandbox Code Playgroud)
为什么我总是得到一个匹配请求错误的多个动作?
我拥有深厚的 Java/Spring 背景,并试图将一些知识转移到 ASP.NET Core 6。
在 Spring 上RestController,我能够根据查询参数的存在来路由请求。
HttpRequest因此,带有 uri: 的a/students?firstName=Kevin可以路由到与HttpRequest带有 uri: 的a 不同的控制器方法/students。
在 ASP.NET Core 6 中,在完成一些示例并阅读 Web API 文档后,我无法确定是否可以实现同等功能。
这是我想要实现的目标,是否可以使用两种方法和路由配置来根据查询参数识别要调用哪个控制器方法?
[ApiController]
[Route("Students")]
public class StudentHomeProfileController : ControllerBase
{
[HttpGet] //Route here when no parameters provided
public async Task<ActionResult<IEnumerable<Student>>> GetStudentAsync()
{
/* Code omitted */
}
[HttpGet] //Route here when firstName query param provided
public async Task<ActionResult<IEnumerable<Student>>> SearchStudentAsync([FromQuery] string firstName)
{
/* Code omitted */
}
}
Run Code Online (Sandbox Code Playgroud) 我需要创建一个ASP.NET Core, Web API支持多个HttpGet动词的唯一区别是查询字符串,但似乎查询字符串不能成为路由模板的一部分 - 是真的吗?
路径模板非常相似,实际上它们只有查询字符串不同.
[Authorize]
public class SymbolsController : Controller
{
[
HttpGet,
Route("api/symbols")
]
public Task<IEnumerable<Symbol>> Symbols([FromServices] ISymbolService symbolService)
{
return symbolService.GetSymbolsAsync();
}
[
HttpGet,
Route("api/symbols?{childrenOf=id}")
]
public Task<IEnumerable<Symbol>> ValidChildren(
[FromQuery] Guid id,
[FromServices] ISymbolService symbolService)
{
return symbolService.GetValidChildrenAsync(id);
}
}
Run Code Online (Sandbox Code Playgroud)
这会引发异常,因为?它不是路由模板中的有效字符.我怎样才能做到这一点?
routing asp.net-web-api asp.net-web-api2 asp.net-core asp.net-core-webapi
我一直在Web.Api中查看路由并查看表示端点的各种方法.我遇到了Instagrams REST端点.(它有一些可爱的文档)使用Web.Api什么是设置像Instagrams用户端点的情况的路由和控制器的最佳方法?
User Endpoints
GET/users/user-id Get basic information about a user.
GET/users/self/feed See the authenticated user's feed.
GET/users/user-id/media/recent Get the most recent media published by a user.
GET/users/self/media/liked See the authenticated user's list of liked media.
GET/users/search Search for a user by name.
Run Code Online (Sandbox Code Playgroud)
如果我想在我的应用程序中复制这些端点,我将如何处理它.我只需要一个带有5种方法的控制器"用户",我需要什么样的路由才能将REST调用指向这些方法?
asp.net-mvc asp.net-mvc-routing asp.net-web-api asp.net-web-api-routing