如何使用子路径设置URL路由

发布于 2025-02-05 14:20:34 字数 191 浏览 1 评论 0原文

我是WebAPI和MVC的新手。如果我想将服务URL分组为

  • /api/account/create
  • /api/account/login
  • /api/account/resetpass

,我能够将所有3个方法调用放在同一控制器文件中,并以某种方式将特定请求映射到正确的方法?

i am new to webapi and MVC in general. If I wanted to group my service URLs like this

  • /api/account/create
  • /api/account/login
  • /api/account/resetpass

Am I able to put all 3 method calls in the same controller file and somehow map a particular request to the right method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗心遗梦遗幸福 2025-02-12 14:20:34

创建一个名为帐户的控制器,然后创建3 [获取,发布,put,删除]方法,并将其命名创建,登录,resetpass
默认情况下,这是MVC / API的路由(ID可以是可选的)
路由模板:“ API/{Controller}/{ID}”,

示例:


public class AccountController : ApiController
{
        [HttpPost]
        public string Create()
        {
            // CODE
        }

        [HttpPost] // or  [HttpGet] 
        public string Login ()
        {
            // CODE
        }

        [HttpPost]
        public string Resetpass()
        {
            // CODE
        }
}


如果您打电话给它们,请尝试给他们特定的路线:

        [HttpGet("GetSubject/{subject}")]
        public int GetSubjectId(String subject)
        {

          //CODE
        }

如果您遇到任何错误或误解,请don'犹豫发表评论

Create a Controller named Account and Create 3 [GET, POST, PUT, DELETE] method and name them create , login ,resetpass.
By Default, this is the routing for MVC / API(Id can be optional)
route Template: "api/{controller}/{id}",

Example :


public class AccountController : ApiController
{
        [HttpPost]
        public string Create()
        {
            // CODE
        }

        [HttpPost] // or  [HttpGet] 
        public string Login ()
        {
            // CODE
        }

        [HttpPost]
        public string Resetpass()
        {
            // CODE
        }
}


if you had trouble calling them, try to give them a specific route :

        [HttpGet("GetSubject/{subject}")]
        public int GetSubjectId(String subject)
        {

          //CODE
        }

Please if you get any error or misunderstanding, don't hesitate to post a comment

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文