Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP MVC Redirect without changing URL(routing)

Goal: I want to be able to type URL: www.mysite.com/NewYork OR www.mysite.com/name-of-business

Depending on the string I want to route to different actions without changing the URL.

So far I have:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapRoute(
        "UrlRouter", // Route name 
        "{query}", // URL with parameters 
        new { controller = "Routing", action = "TestRouting" } // Parameter defaults
    );
}

In the controller I have:

public ActionResult TestRouting(string query)
{
    if (query == "NewYork")
        return RedirectToAction("Index", "Availability");    // <--------- not sure
    else if (query == "name-of-business")
        return Redirect("nameofbusines.aspx?id=2731");       // <--------- not sure
    else
        return RedirectToAction("TestTabs", "Test");         // <--------- not sure
}

I have pretty much tried everything to redirect/transfer to the page without changing the URL, but everything I've tried changes the URL or gives me an error.

Basically I'm looking for the equivalent of server.transfer where I can keep the URL but send info to the action and have it display its result.

like image 647
CesarHerrera Avatar asked Apr 15 '09 17:04

CesarHerrera


2 Answers

I'm with Nick on this one, though I think you could just use regular views instead of having to do partials. You may need to implement them as shared views if they are not in the views corresponding to the controller (since it will only look in the associated and shared views).

public ActionResult TestRouting(string query)
{
    if (query == "NewYork")
    {
        var model = ...somehow get "New York" model
        return View("Index", model );
    }
    else if (query == "name-of-business")
    {
        var model = ...get "nameofbusiness" model
        return View("Details", model );
    }
    else
    {
        return View("TestTabs");
    }
}

Each view would then take a particular instance of the model and render it's contents using the model. The URL will not change.

Anytime that you use a RedirectResult, you will actually be sending an HTTP redirect to the browser and that will force a URL change.

like image 57
tvanfosson Avatar answered Oct 08 '22 20:10

tvanfosson


Im not sure if you tried this way or if this way has any drawbacks..

Add a global.asax file to your project. In that add the following method:

void Application_BeginRequest(object sender, EventArgs e)
{
    // Handles all incoming requests
    string strURLrequested = Context.Request.Url.ToString();
    GetURLToRedirect objUrlToRedirect = new GetURLToRedirect(strURLrequested); 
    Context.RewritePath(objUrlToRedirect.RedirectURL);
}

GetURLToRedirect can be a class that has the logic to find the actual URL based on the URL typed in. The [RedirectURL] property will be set with the url to redirect to beneath the sheets.

Hope that helps...

like image 28
user20358 Avatar answered Oct 08 '22 20:10

user20358