C# 在 Web API 中使用 RedirectToAction
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11737040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use RedirectToAction in Web API
提问by Thilok Gunawardena
I am using RedirectToAction in my ASP.Net WebAPI application and I tried the following one.
我在我的 ASP.Net WebAPI 应用程序中使用 RedirectToAction 并尝试了以下一个。
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
But, since I am using WebAPI, I need to be the URL like below.
但是,因为我使用的是 WebAPI,所以我需要像下面这样的 URL。
127.0.0.1:81/api/authenticationservicewebapi/authenticateuser/admin/admin@123
How do I do this?
我该怎么做呢?
采纳答案by Filip W
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?)
我不确定您的路由、Web API 操作签名如何,所以我会尝试猜测。一些事情并没有真正加起来(为什么要在 url 中传递密码?)
but...
但...
Given your url structure I'd guess your routing is something like:
鉴于您的 url 结构,我猜您的路由类似于:
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}/{id2}",
defaults: new { id = RouteParameter.Optional, id2 = RouteParameter.Optional }
);
Then given that, I guess your authenticateuser must be something like:
然后考虑到这一点,我猜你的身份验证用户必须是这样的:
public HttpResponseMessage AuthenticateUser([FromUri]string id, [FromUri]string id2)
If so, then to redirect to this from an MVC controller you need:
如果是这样,那么要从 MVC 控制器重定向到它,您需要:
return Redirect(
Url.RouteUrl("DefaultApi",
new { httproute = "",
controller = "AuthenticationServiceWebApi",
action = "AuthenticateUser",
id = model.UserName,
id2 = model.Password
}));
回答by Aliostad
If the user is not authenticated you should not redirect. There usually is no interactive user at the other end so you should really return HTTP Status Code 401 instead of redirect.
如果用户未通过身份验证,则不应重定向。另一端通常没有交互式用户,因此您应该真正返回 HTTP 状态代码 401 而不是重定向。
There is no equivalent in ASP.NET Web API.
ASP.NET Web API 中没有等效项。
If you insistdoing it then here it is:
如果你坚持这样做,那么这里是:
You throw HttpResponseException:
你扔HttpResponseException:
var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.Found);
httpResponseMessage.Headers.Location = new Uri(myAddress, UriKind.Relative);
throw new HttpResponseException(httpResponseMessage);
回答by Thilok Gunawardena
I also came up with a simple solution. Not very good as the above, but worth sharing.
我还想出了一个简单的解决方案。不如上面的好,但值得分享。
string post_data = "userName=th&password=admin@123";
string uri = "http://127.0.0.1:81/api/authenticationservicewebapi/authenticateuser";
// create a request
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(uri);
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";
// turn our request string into a byte stream
byte[] postBytes = Encoding.ASCII.GetBytes(post_data);
// this is important - make sure you specify type this way
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postBytes.Length;
Stream requestStream = request.GetRequestStream();
// now send it
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
Thank You all.
谢谢你们。

