CreatedAtRoute路由到其他控制器

编程入门 行业动态 更新时间:2024-10-28 04:29:06
本文介绍了CreatedAtRoute路由到其他控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用属性路由创建一个新的webapi来像这样创建嵌套路由:

I'm creating a new webapi using attribute routing to create a nested route as so:

// PUT: api/Channels/5/Messages [ResponseType(typeof(void))] [Route("api/channels/{id}/messages")] public async Task<IHttpActionResult> PostChannelMessage(int id, Message message) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != message.ChannelId) { return BadRequest(); } db.Messages.Add(message); await db.SaveChangesAsync(); return CreatedAtRoute("DefaultApi", new { id = message.Id }, message); }

但是我想返回未嵌套的路线,即:

I however want to return a route that isn't nested i.e.:

/api/Messages/{id}

在消息控制器上定义的

.但是,上面的CreatedAtRoute调用不能解析此路由,而是抛出.我做错了什么,还是不支持路由到其他api控制器? n.b.我尝试打的路由不是属性路由,只是默认路由.

which is defined on the messages controller. However The CreatedAtRoute call above is not resolving this route and instead throwing. Have I done something wrong, or does it not support routing to different api controller? n.b. the route I am trying to hit is not an attribute route, just a default one.

例外是:

消息:发生错误." ExceptionMessage:"UrlHelper.Link不能返回null." ExceptionType:"System.InvalidOperationException" StackTrace:位于System.Web.Http.Results.CreatedAtRouteNegotiatedContentResult 1.Execute() at System.Web.Http.Results.CreatedAtRouteNegotiatedContentResult 1.ExecuteAsync(CancellationToken cancellingToken),位于System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()---从上一个位置开始的堆栈跟踪结尾在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的System.Runtime.CompilerServices.TaskAwaiter 1.GetResult() at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()处引发了异常在System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()"

Message: "An error has occurred." ExceptionMessage: "UrlHelper.Link must not return null." ExceptionType: "System.InvalidOperationException" StackTrace: " at System.Web.Http.Results.CreatedAtRouteNegotiatedContentResult1.Execute() at System.Web.Http.Results.CreatedAtRouteNegotiatedContentResult1.ExecuteAsync(CancellationToken cancellationToken) at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()"

如果它不支持此功能,那么返回201的规范方法是什么?我可以安全地重构它吗?

If it doesn't support this, what is the canonical way to return a 201 and can I do it in a refactor safe way?

推荐答案

哦,亲爱的,这可能是回答我自己的问题的新记录.

Oh dear, this may be a new record for answering my own question.

return CreatedAtRoute("DefaultApi", new { controller = "messages", id = message.Id }, message);

可以解决问题.即明确指定控制器.我通过查看异常与UrlHelper有关并阅读其文档来解决此问题.

does the trick. i.e. explicitly specifying the controller. I worked this our by seeing that the exception was related to the UrlHelper and reading its docs...

更多推荐

CreatedAtRoute路由到其他控制器

本文发布于:2023-10-26 10:51:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529921.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   控制器   CreatedAtRoute

发布评论

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

>www.elefans.com

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