I want to redirect to another .aspx
page from WebAPI
. I have used this code but it is not working:
string url = "http://localhost:61884/UserList.aspx";
System.Uri uri = new System.Uri(url);
return Redirect(uri).ToString();
答案 0 :(得分:2)
你没有。 (或您对问题的描述不准确)
Web API旨在检索数据或保留数据,它是一种从客户端与服务器交互的方式,而无需执行传统的表单发布或页面请求调用。调用者(基于您的问题标签angularJs的javascript)需要在检索到对Web API的调用结果后执行重定向。
总结:Web API代码本身不应该是客户端的任何类型的重定向。客户应该处理这个问题。
调用web api并从角度代码重定向:
$http({
url: "/api/SomeWebApiUrl",
data: {},
method: "POST",
headers: { 'Content-Type': "application/json" },
responseType: "json"
}).then(function (response) {
if(response.data.somethingToCheck === someConditionThatWarrentsRedirect)
$window.location.href = "~/someOtherUrl/";
});
答案 1 :(得分:0)
尝试这样的事情:
var response = Request.CreateResponse(HttpStatusCode.Moved);
string fullyQualifiedUrl = Request.RequestUri.GetLeftPart(UriPartial.Authority);
response.Headers.Location = new Uri(fullyQualifiedUrl);
希望有所帮助。
答案 2 :(得分:-1)
Redirect from asp.net web api post action
public HttpResponseMessage Post()
{
// ... do the job
// now redirect
var response = Request.CreateResponse(HttpStatusCode.Moved);
response.Headers.Location = new Uri("http://www.abcmvc.com");
return response;
}