ASP MVC重定向而不更改URL(路由)

编程入门 行业动态 更新时间:2024-10-22 16:28:27
本文介绍了ASP MVC重定向而不更改URL(路由)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有一个小问题,完成这个IM,希望有人能帮助我。目前使用C#。

im having a little problem accomplishing this, hopefully someone can help me out. Currently using c#.

目标:我希望能够输入网址: www.mysite/NewYork要么 www.mysite/name-of-business

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

根据字符串我想路由到不同的操作不改变网址。

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

到目前为止,我有:在:公共静态无效的RegisterRoutes(RouteCollection路线)

So far i have: In: public static void RegisterRoutes(RouteCollection routes)

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

在控制器我有:

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 }

我有pretty多竭尽所能来重定向/转移到页面,而不更改URL,但一切我试过改变URL或给我一个错误。

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.

基本上即时寻找的Server.Transfer相当于在那里我可以保持网址但发送信息的行为,并把它显示它的结果。

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

任何投入将是AP preciated。

Any input would be appreciated.

推荐答案

我和尼克是在这一个,但我认为你可以只使用,而不必做定期的谐音意见。您可能需要实现它们的共享视图,如果他们没有在相应的控制器(因为它只会在关联和共享视图看)的意见。

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.

您使用RedirectResult任何时候,你实际上将发送一个HTTP重定向到浏览器,这将迫使一个URL的变化。

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

更多推荐

ASP MVC重定向而不更改URL(路由)

本文发布于:2023-10-31 13:01:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546161.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不   路由   重定向   ASP   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!