<legend id='sAELD'><style id='sAELD'><dir id='sAELD'><q id='sAELD'></q></dir></style></legend>

      <tfoot id='sAELD'></tfoot>

        <bdo id='sAELD'></bdo><ul id='sAELD'></ul>

      <small id='sAELD'></small><noframes id='sAELD'>

      1. <i id='sAELD'><tr id='sAELD'><dt id='sAELD'><q id='sAELD'><span id='sAELD'><b id='sAELD'><form id='sAELD'><ins id='sAELD'></ins><ul id='sAELD'></ul><sub id='sAELD'></sub></form><legend id='sAELD'></legend><bdo id='sAELD'><pre id='sAELD'><center id='sAELD'></center></pre></bdo></b><th id='sAELD'></th></span></q></dt></tr></i><div id='sAELD'><tfoot id='sAELD'></tfoot><dl id='sAELD'><fieldset id='sAELD'></fieldset></dl></div>

        为特定操作创建不同的路线

        时间:2023-07-11

            • <bdo id='mapwn'></bdo><ul id='mapwn'></ul>
                <tbody id='mapwn'></tbody>

              <tfoot id='mapwn'></tfoot>
              • <small id='mapwn'></small><noframes id='mapwn'>

                <legend id='mapwn'><style id='mapwn'><dir id='mapwn'><q id='mapwn'></q></dir></style></legend>

                  <i id='mapwn'><tr id='mapwn'><dt id='mapwn'><q id='mapwn'><span id='mapwn'><b id='mapwn'><form id='mapwn'><ins id='mapwn'></ins><ul id='mapwn'></ul><sub id='mapwn'></sub></form><legend id='mapwn'></legend><bdo id='mapwn'><pre id='mapwn'><center id='mapwn'></center></pre></bdo></b><th id='mapwn'></th></span></q></dt></tr></i><div id='mapwn'><tfoot id='mapwn'></tfoot><dl id='mapwn'><fieldset id='mapwn'></fieldset></dl></div>
                  本文介绍了为特定操作创建不同的路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在开发一个 asp.net 5 mvc api,我目前正在开发 Accounts Controller.

                  I am working on an asp.net 5 mvc api, and I am currently working on the Accounts Controller.

                  因为我在许多不同的地方看到了使用 /api/Token 路由到 Web api 登录的约定.我想路由到没有帐户前缀的特定方法,我不想使用不同的控制器,并且我更喜欢在 Startup.cs 中使用属性而不是路由以避免将来出现混淆.

                  since I saw in many different places that there is a convention of using /api/Tokenrouting to a login in a web api. I would like to route to that specific method without the accounts prefix, I would prefer not using a different controller, and I would prefer using Attributes over routing in Startup.cs to avoid confusion in the future.

                  这是我目前拥有的

                  [Route("api/[controller]")]
                  public class AccountsController : Controller
                  {
                      [HttpPost("login")]
                      public async Task<JwtToken> Token([FromBody]Credentials credentials)
                      {
                       ...
                      }
                  
                      [HttpPost]
                      public async Task CreateUser([FromBody] userDto)
                      {
                        ...
                      }
                  }
                  

                  推荐答案

                  使用属性路由,您可以在 Action 的路由属性上使用 波浪号 (~)如果需要,覆盖 Controller 的默认路由:

                  With attribute routing you can use a tilde (~) on the Action's route attribute to override the default route of the Controller if needed:

                  [Route("api/[controller]")]
                  public class AccountsController : Controller {
                  
                      [HttpPost]
                      [Route("~/api/token")] //routes to `/api/token`
                      public async Task<JwtToken> Token([FromBody]Credentials credentials) {
                          ...
                      }
                  
                      [HttpPost] 
                      [Route("users")] // routes to `/api/accounts/users`
                      public async Task CreateUser([FromBody] userDto) {
                          ...
                      }
                  }
                  

                  这篇关于为特定操作创建不同的路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 ASP.NET Core 中流式传输文件后删除文件 下一篇:JsonSerializer.Deserialize 失败

                  相关文章

                    <bdo id='keB4Q'></bdo><ul id='keB4Q'></ul>

                  1. <small id='keB4Q'></small><noframes id='keB4Q'>

                    <legend id='keB4Q'><style id='keB4Q'><dir id='keB4Q'><q id='keB4Q'></q></dir></style></legend>
                    <i id='keB4Q'><tr id='keB4Q'><dt id='keB4Q'><q id='keB4Q'><span id='keB4Q'><b id='keB4Q'><form id='keB4Q'><ins id='keB4Q'></ins><ul id='keB4Q'></ul><sub id='keB4Q'></sub></form><legend id='keB4Q'></legend><bdo id='keB4Q'><pre id='keB4Q'><center id='keB4Q'></center></pre></bdo></b><th id='keB4Q'></th></span></q></dt></tr></i><div id='keB4Q'><tfoot id='keB4Q'></tfoot><dl id='keB4Q'><fieldset id='keB4Q'></fieldset></dl></div>

                    1. <tfoot id='keB4Q'></tfoot>