ASP MVC Redirect without changing URL (routing)

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

Depending on the line, I want to redirect to different actions without changing the URL.

So far I:

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 tried almost everything to redirect / redirect to the page without changing the URL, but everything I tried changes the URL or gives me an error.

Basically I am looking for the equivalent of server.transfer, where I can save the URL, but send the information to the action and show its result.

+6
c # asp.net-mvc url-routing
source share
5 answers

I'm with Nick on this, although I think you could just use regular views instead of doing partial ones. You may need to implement them as general views if they are not in the views corresponding to the controller (since they will only be displayed in related and general 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 then takes a specific instance of the model and displays its contents using the model. URL will not change.

Any time you use RedirectResult, you will actually send HTTP redirects to the browser, and this will change the URL.

+5
source share

You can change your controller as follows:

 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 as follows:

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

What is it. And you can improve this example with a class and some functions.

+2
source share

You want to say you want to go to "www.mysite.com/NewYork" and then "really" go "somewhere else", but leave the URL alone? Perhaps what you would like to do is use partial views to implement this? So your base page will be where you are heading, and then inside this page you do your state testing to reveal different partial views? I did this in my application to view either the readable version of the grid or the editable grid. He worked very well.

0
source share

I'm not sure what you can do with a redirect to the .aspx page, but you should be able to replace RedirectToAction(...)s 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(); } } 
0
source share

I am not sure if you tried this way or if it has any flaws.

Add the global.asax file to your project. In this case, 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 may be a class that has logic for finding the actual URL based on the URL. The [RedirectURL] property will be set with the redirect URL below the sheets.

Hope this helps ...

0
source share

All Articles