<tfoot id='1koBi'></tfoot>

    1. <legend id='1koBi'><style id='1koBi'><dir id='1koBi'><q id='1koBi'></q></dir></style></legend>
        <bdo id='1koBi'></bdo><ul id='1koBi'></ul>
      <i id='1koBi'><tr id='1koBi'><dt id='1koBi'><q id='1koBi'><span id='1koBi'><b id='1koBi'><form id='1koBi'><ins id='1koBi'></ins><ul id='1koBi'></ul><sub id='1koBi'></sub></form><legend id='1koBi'></legend><bdo id='1koBi'><pre id='1koBi'><center id='1koBi'></center></pre></bdo></b><th id='1koBi'></th></span></q></dt></tr></i><div id='1koBi'><tfoot id='1koBi'></tfoot><dl id='1koBi'><fieldset id='1koBi'></fieldset></dl></div>
    2. <small id='1koBi'></small><noframes id='1koBi'>

    3. 在 ASP MVC 6 中将多个路由分配给同一控制器或操作

      时间:2023-06-08
          <tbody id='yfrBX'></tbody>
            <i id='yfrBX'><tr id='yfrBX'><dt id='yfrBX'><q id='yfrBX'><span id='yfrBX'><b id='yfrBX'><form id='yfrBX'><ins id='yfrBX'></ins><ul id='yfrBX'></ul><sub id='yfrBX'></sub></form><legend id='yfrBX'></legend><bdo id='yfrBX'><pre id='yfrBX'><center id='yfrBX'></center></pre></bdo></b><th id='yfrBX'></th></span></q></dt></tr></i><div id='yfrBX'><tfoot id='yfrBX'></tfoot><dl id='yfrBX'><fieldset id='yfrBX'></fieldset></dl></div>

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

            • <bdo id='yfrBX'></bdo><ul id='yfrBX'></ul>
            • <tfoot id='yfrBX'></tfoot>
            • <legend id='yfrBX'><style id='yfrBX'><dir id='yfrBX'><q id='yfrBX'></q></dir></style></legend>

                本文介绍了在 ASP MVC 6 中将多个路由分配给同一控制器或操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                问题:

                有没有办法在 ASP.NET MVC 6 应用程序中将两个不同的路由(带参数)分配给同一个控制器?

                Is there any way to assign two different routes (with parameters) to the same controller in an ASP.NET MVC 6 application?

                我试过了:

                我尝试对控制器类和单个操作使用多个路由属性,但没有奏效.

                I tried using multiple route attributes to the controller class and also to the individual actions, did not work.

                注意事项:

                • 我正在使用 ASP.NET Core 1.0 RC1.

                • I am using ASP.NET Core 1.0 RC1.

                我想这样做的原因是,我希望 api 与我们使用旧 URL 的旧版本的移动应用程序兼容.

                示例:

                [Produces("application/json")]
                [Route("api/v2/Log")]
                /// The old route is "api/LogFile" which I want to be still valid for this controller.
                public class LogController : Controller {
                    [HttpGet("{id}", Name = "download")]
                    public IActionResult GetFile([FromRoute] Guid id) 
                    {
                        // ...
                    }
                }
                

                在上面的示例中:api/LogFile/{some-guid} 是旧路由,api/v2/log/download/{some-guid} 是新路线.我需要两条路线调用相同的操作.

                In the example above: api/LogFile/{some-guid} is the old route and api/v2/log/download/{some-guid} is the new route. I need both routes invoke the same action.

                推荐答案

                在控制器级别拥有 2 个路由属性在新的 RC1 应用程序中可以正常工作:

                Having 2 route attributes at the controller level works fine in a new RC1 application:

                [Produces("application/json")]
                [Route("api/[controller]")]
                [Route("api/old-log")]
                public class LogController: Controller
                {
                    [HttpGet]
                    public IActionResult GetAll()
                    {
                        return Json(new { Foo = "bar" });
                    }
                }
                

                http://localhost:62058/api/loghttp://localhost:62058/api/old-log 都返回预期的 json.我看到的唯一警告是,您可能需要设置属性的名称/顺序属性,以防您需要为其中一项操作生成 url.

                Both http://localhost:62058/api/log and http://localhost:62058/api/old-log return the expected json. The only caveat I have seen is that you might want to set the name/order properties of the attributes in case you need to generate the url to one of those actions.

                在动作上有 2 个属性也可以:

                Having 2 Attributes on the action also works:

                [Produces("application/json")]        
                public class LogController : Controller
                {
                    [Route("api/old-log")]
                    [Route("api/[controller]")]
                    [HttpGet]
                    public IActionResult GetAll()
                    {
                        return Json(new { Foo = "bar" });
                    }
                }
                

                但是,在控制器级别拥有通用路由和特定操作路由时需要小心.在这些情况下,控制器级别的路由用作前缀并添加到 url(有一篇关于此行为的好文章 这里).这可能会为您提供一组与您预期不同的 url,例如:

                However you need to be careful when having a general route at the controller level and a specific action route. In these cases the route at the controller level is used as a prefix and prepended to the url (There is a nice article about this behavior here). This might get you a different set of urls than you were expecting, for example with:

                [Produces("application/json")]
                [Route("api/[controller]")]
                public class LogController : Controller
                {
                    [Route("api/old-log")]
                    [Route("")]
                    [HttpGet]
                    public IActionResult GetAll()
                    {
                        return Json(new { Foo = "bar" });
                    }
                }
                

                在最后一种情况下,您的应用程序将侦听的 2 个路由将是 http://localhost:62058/api/loghttp://localhost:62058/api/log/api/old-log 因为 api/log 作为前缀添加到在操作级别定义的所有路由.

                The 2 routes that your application will listen in the last case will be http://localhost:62058/api/log and http://localhost:62058/api/log/api/old-log since api/log is added as prefix to all the routes defined at the action level.

                最后,另一种选择是为新路由使用属性,然后使用启动类中的路由表来提供处理旧 API 的特定路由.

                Finally, another option would be to use attributes for your new routes and then use the route table in the startup class to provide specific routes that take care of the old api.

                这篇关于在 ASP MVC 6 中将多个路由分配给同一控制器或操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在 ASP.NET Core 1.0 中获取 bin 文件夹 下一篇:在 ASP.NET Core MVC 中自定义响应序列化

                相关文章

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

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

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