Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to new url C# MVC with HttpResponse.Redirect

Tags:

c#

asp.net-mvc

I am struggling with the HttpResponse.Redirect method. I thought it would be included in System.Web but I am getting the

The name 'Response' does not exist in the current context" error.

This is the entire controller:

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,
            };
        }
    }
}
like image 789
Niko Avatar asked Oct 10 '13 11:10

Niko


People also ask

How do I redirect a URL to another URL?

Click the URL Redirects tab. In the upper right, click Add URL redirect. In the right panel, select the Standard or Flexible redirect type. A standard redirect is used to redirect one URL to another.

How do I redirect in JSTL?

The < c:redirect > tag redirects the browser to a new URL. It supports the context-relative URLs, and the < c:param > tag. It is used for redirecting the browser to an alternate URL by using automatic URL rewriting.

How redirect URL from action method in MVC?

You can use any of the following methods to return a RedirectResult: Redirect – Http Status Code 302 Found (temporarily moved to the URL provided in the location header) RedirectPermanent – Http Status Code 301 Moved Permanently. RedirectPermanentPreserveMethod – Http Status Code 308 Permanent Redirect.

How do I redirect to another controller?

In this blog you will learn how to Redirect from One Controller Action to Another. Step1: Create an ASP.net MVC project. Choose ASP.Net MVC project from template and Press Next, then name the empty project as RoutingExample and click ok. Step 2: Add two controllers.


2 Answers

You can get HttpResponse object for current request in Your action method using the following line:

HttpContext.Current.Response

and so You can write:

HttpContext.Current.Response.Redirect("http://www.google.com");  

Anyway, You use HttpResponseMessage, so the proper way to redirect would be like this:

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;
}
like image 196
Aleksei Chepovoi Avatar answered Sep 20 '22 18:09

Aleksei Chepovoi


In an MVC web application controller, Response isn't accessed in the same way as it would be from an aspx page. You need to access it through the current http context.

HttpContext.Current.Response.Redirect("http://www.google.com");
like image 26
Tom Bowers Avatar answered Sep 22 '22 18:09

Tom Bowers