1

Is there a way to rewrite the path displaying a different view instead of RedirectToAction or Redirect? I don't want the url to change, but notice the regular RewritePath cannot be "returned", so the action actually keeps going after it!

public ActionResult Register(){
    if (somehting){
        HttpContext.RewritePath(url);
        // I want it to stop here, somehow, but it keeps going and expects a return statement
    }
    return View();
}

Update: I just noticed when I use the one circulating in stackoverflow (with ProcessRequest) if I run F5 it works, but when I access directly it gives me the "'HttpContext.SetSessionStateBehavior' can only be invoked before 'HttpApplication.AcquireRequestState' event is raised." which means im missing something in IIS setup, what is it? :s

[Update] To be clear on this, I need to rewrite a "url" not an action or view name, it must be a url, like good old RewritePath(url)

Ayyash
  • 4,257
  • 9
  • 39
  • 58
  • juke like the error handler in MVC works, it rewrites to error page – Ayyash Jun 02 '12 at 10:19
  • noone got the answer, and i still dont know how to rewrite a path on MVC, it was so damn easy in ASP.NET! im pretty sure it has to do with the order of objects built in MVC, first the route is called, then the controller, then something that makes the response flush, disallowing rewrite path from taking effect... still, there must be a way – Ayyash Jun 06 '12 at 07:19
  • what am i gonna do with this question? there are no answers, should i delete? – Ayyash Jun 18 '12 at 05:03

4 Answers4

1

For only action redirect:

Use sample return RedirectToAction("Action","Controller",new {id=1});

For Url redirect:

Can use return Redirect(URL);

also expression after HttpContext.RewritePath(url); use response.end();

B. Bilgin
  • 774
  • 12
  • 22
  • 1
    the redirection schemes change urls, i dont want that, and i did try response.end, the response seriously ended but it did not redirect to anything, am i missing something? – Ayyash May 27 '12 at 09:33
1

This is what I finally did, inspired by Stephen's answer to create a method that handles the returned views:

First the global method (here its named Timeout in Home controller) under HomeController.cs:

public ActionResult TimeOut(){
    return View("TimeOut"); // the string is essential
}

else where:

if (soemthing){
    return new HomeController().TimeOut(); }

any objections? wrong doings?

Ayyash
  • 4,257
  • 9
  • 39
  • 58
0

Return a different view with a different model and the url won't change.

like this let's say you have a view called Hello.cshtml.

return View('Hello',Model);

So you return a different View than the action, the url stay the same, the view change for the user.

Kuqd
  • 497
  • 3
  • 8
0

In addition to Cyril's answer, you could directly call another Controller method and return the result of that method. Let's say you have a method as follows that returns what you want to return.

    public ActionResult SpecialRegistration()
    {
       //TODO:  implement logic that does stuff
       return View();  // Shows the View named SpecialRegistration
    }

Now in your regular Register method you can just call it

    public ActionResult Register(){
        if (something){
            return SpecialRegistration();
        }
   }
stephen.vakil
  • 3,492
  • 1
  • 18
  • 23
  • that means they must be in the same controller, too restrictive – Ayyash May 31 '12 at 09:11
  • i just tried this, oddly enough, calling SpecialRegistration returns Register View as it is (register.cshtml), makes sense? – Ayyash May 31 '12 at 09:24
  • Inspired by this answer, I found out how to go around it but it isnt as clean as I wished it to be... see answer – Ayyash May 31 '12 at 09:28