我正在努力解决这个问题HttpResponse.Redirect.我以为它会包括在内System.Web但我得到了
"响应"名称在当前上下文中不存在"错误.
这是整个控制器:
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http;
namespace MvcApplication1.Controllers
{
public class SmileyController : ApiController
{
public HttpResponseMessage Get(string id)
{
Response.Redirect("http://www.google.com");
return new HttpResponseMessage
{
Content = new StringContent("[]", new UTF8Encoding(), "application/json"),
StatusCode = HttpStatusCode.NotFound,
};
}
}
}
Run Code Online (Sandbox Code Playgroud)
Ale*_*voi 20
您可以使用以下行在您的操作方法中获取当前请求的HttpResponse对象:
HttpContext.Current.Response
Run Code Online (Sandbox Code Playgroud)
所以你可以写:
HttpContext.Current.Response.Redirect("http://www.google.com");
Run Code Online (Sandbox Code Playgroud)
无论如何,你使用HttpResponseMessage,所以正确的重定向方式是这样的:
public HttpResponseMessage Get(string id)
{
// Your logic here before redirection
var response = Request.CreateResponse(HttpStatusCode.Moved);
response.Headers.Location = new Uri("http://www.google.com");
return response;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28026 次 |
| 最近记录: |