获取ASP.NET核心的路线结果

发布于 2025-02-06 08:43:36 字数 578 浏览 2 评论 0 原文

我正在研究一个ASP.NET COE Web API项目。

我有一个 post 方法(路由),我需要它来调用另一个 get 方法:

[HttpGet(Name = "Profile")]
public async Task<ActionResult<ProfileResponseDto>> GetProfile(string username)

如果我致电:

return RedirectToRoute("Profile", new { username = profile.UserName });

我最终将获得 get resource 。

但是我想要的是采用该 get 方法的结果,然后继续以我的帖子方法。

在python中

amount = requests.get("http://"+catalogIpAddress+":"+port+"/count/" + str(id))

I'm working on an ASP.NET Coe Web API project.

I have a post method (route) and I need it to call another get method:

[HttpGet(Name = "Profile")]
public async Task<ActionResult<ProfileResponseDto>> GetProfile(string username)

If I call :

return RedirectToRoute("Profile", new { username = profile.UserName });

I will end up with Get resource.

But what I want is to take the result of that Get method and continue in my post method.

In Python its something like

amount = requests.get("http://"+catalogIpAddress+":"+port+"/count/" + str(id))

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

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

发布评论

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

评论(1

尐籹人 2025-02-13 08:43:39

httpresponse.redirecttoroute方法通过使用路由参数值,路由名称或两者都将请求重定向到新URL,这意味着您无法继续使用最后一个url

httpresponse.redirecttoroute方法

但是您可以从其他动作结果中返回值,

var result = await controller.GetTodosAsync();

以便您可以使用类似的结果。这 :

        [HttpGet(Name = "Profile")]
        public async Task<string> GetProfile(string username)
        {
           return await Task.Run(()=> "hello") ;
        }

        [HttpPost(Name = "Profile")]
        public async Task<string> PostProfile(string username)
        {
            string result = await this.GetProfile(username);

            result = result + " from post";
            return await Task.Run(() => result);
        }

HttpResponse.RedirectToRoute Method Redirects a request to a new URL by using route parameter values, a route name, or both this means that you cannot continue with the last URL

HttpResponse.RedirectToRoute Method

but you can return value from another action result like

var result = await controller.GetTodosAsync();

so you can use like this :

        [HttpGet(Name = "Profile")]
        public async Task<string> GetProfile(string username)
        {
           return await Task.Run(()=> "hello") ;
        }

        [HttpPost(Name = "Profile")]
        public async Task<string> PostProfile(string username)
        {
            string result = await this.GetProfile(username);

            result = result + " from post";
            return await Task.Run(() => result);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文