使用RedirectToAction在网页API [英] Use RedirectToAction in Web API

查看:320
本文介绍了使用RedirectToAction在网页API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用RedirectToAction在我的ASP.Net应用程序的WebAPI和我尝试以下之一。

I am using RedirectToAction in my ASP.Net WebAPI application and I tried the following one.

return RedirectToAction("AuthenticateUser", "AuthenticationServiceWebApi", new RouteValueDictionary
                                                                    {
                                                                        {"userName", model.UserName},
                                                                        {"password", model.Password}
                                                                    });

这会产生如下的重定向。

This generates the redirection as below.

127.0.0.1:81/authenticationservicewebapi/authenticateuser/admin/admin@123

但是,因为我使用的WebAPI,我需要像下面的网址。

But, since I am using WebAPI, I need to be the URL like below.

127.0.0.1:81/api/authenticationservicewebapi/authenticateuser/admin/admin@123

我如何做到这一点?
在先进的感谢。

How do I do this? Thanks in advanced.

推荐答案

我不知道怎么你的路由,网络API操作签名的样子,所以我会尝试猜测。
有几件事情真的不加起来在这里(为什么你会在url中传递密码?)

I'm not sure how your routing, Web API action signature look like so I will try to guess. A few things don't really add up here (why would you pass the password in the url?)

但...

鉴于你的URL结构我猜你的路由是这样的:

Given your url structure I'd guess your routing is something like:

    routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{action}/{id}/{id2}",
        defaults: new { id = RouteParameter.Optional, id2 = RouteParameter.Optional }
    );

然后因为,我猜你的authenticateUser必须是这样的:

Then given that, I guess your authenticateuser must be something like:

public HttpResponseMessage AuthenticateUser([FromUri]string id, [FromUri]string id2)

如果是这样,那么从一个MVC控制器,你需要重定向到这样的:

If so, then to redirect to this from an MVC controller you need:

        return Redirect(
            Url.RouteUrl("DefaultApi", 
                new { httproute = "", 
                      controller = "AuthenticationServiceWebApi", 
                      action = "AuthenticateUser", 
                      id = model.UserName,
                      id2 = model.Password
            }));

这篇关于使用RedirectToAction在网页API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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