6

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.

budi
  • 6,351
  • 10
  • 55
  • 80
CesarHerrera
  • 483
  • 2
  • 7
  • 13
  • I was actually able to come up with exactly what i was looking for. Its a little complicated but once you get it set up you can pretty much do create the routes on the fly. I'll post my answer soon, this project is rough! – CesarHerrera May 01 '09 at 22:43

5 Answers5

5

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.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • I think i see what youre saying here, basically make the views shared and return the view and pass the view information through the model. this is probably doable, but sadly i suck and did not use model, i pass a few things through ViewData. This seems doable though if i cant find any other answe – CesarHerrera Apr 15 '09 at 19:01
  • I was hoping to find an answer using routing so i can just call the actions with parameters so they can do the work, but this might work on a pinch. – CesarHerrera Apr 15 '09 at 19:05
  • Ah yea, was also hoping to find a way to not have to create more views in shared, just use the existing ones, not sure if thats possible. – CesarHerrera Apr 15 '09 at 19:09
4

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...

user20358
  • 14,182
  • 36
  • 114
  • 186
2

You can change your controller like this:

public ActionResult TestRouting(string query)
{
    string controller,action;

    if (query == "NewYork")
    {
        controller = "Availability";
        action = "Index";
    }
    else
    {
        controller = "Test";
        action = "TestTabs";
    }

    ViewBag.controller = controller;
    ViewBag.action = action;

    return View();
}

Then you can use these ViewBags in your view like this:

@{
    Layout = null;
    Html.RenderAction(ViewBag.action, ViewBag.controller);
}

That's it. And you can improve this example with use a class and some functions.

0

Are you saying you want to go to "www.mysite.com/NewYork" and then "really" go "somewhere else" but leave the url alone? Perhaps what you would want to do then is use partial views to implement this? That way, your base page would be what gets routed to, and then inside of that page you do your condition testing to bring up different partial views? I've done that in my application for viewing either a read-only version of a grid or an editable grid. It worked very nicely.

Nick DeVore
  • 9,748
  • 3
  • 39
  • 41
  • What i want to do is have www.mysite.com/NewYork and www.mysite.com/BusinessName to go different actions. One that handles locations and another one that handles businesses. BUT at the same time keep the URL intact. – CesarHerrera Apr 15 '09 at 18:31
0

I'm not sure what you can do about the redirect to the .aspx page, but you should be able to replace the RedirectToAction(...)s with something like this:

public ActionResult TestRouting(string query)
{
    if (query == "NewYork") 
    {
        var controller = new AvailabilityController();        
        return controller.Index();
    }
    else if (query == "name-of-business")
        return Redirect("nameofbusines.aspx?id=2731");       <--------- not sure
    else 
    {
        var controller = new TestController();        
        return controller.TestTabs();
    }

}
Jon Norton
  • 2,969
  • 21
  • 20
  • Thanks for the reply, but sadly: var controller = new TestController(); return controller.TestTabs(); doesnt work either, It is calling the actual action, but not returning the view results which is what i want to display. I wonder what i can do to get the view html to show – CesarHerrera Apr 15 '09 at 18:56