CreatedAtRoute路由到其他控制器 [英] CreatedAtRoute routing to different controller

查看:134
本文介绍了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路由到其他控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆