C# ASP MVC 重定向而不更改 URL(路由)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/752922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP MVC Redirect without changing URL(routing)
提问by CesarHerrera
Goal: I want to be able to type URL: www.mysite.com/NewYork OR www.mysite.com/name-of-business
目标:我希望能够输入 URL:www.mysite.com/NewYork 或 www.mysite.com/name-of-business
Depending on the string I want to route to different actions without changing the URL.
根据字符串,我想在不更改 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.
我几乎尝试了所有重定向/转移到页面而不更改 URL 的方法,但是我尝试的所有操作都会更改 URL 或给我一个错误。
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.
基本上,我正在寻找相当于 server.transfer 的东西,我可以在其中保留 URL,但将信息发送到操作并让它显示其结果。
回答by Nick DeVore
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.
你是说你想去“www.mysite.com/NewYork”然后“真的”去“其他地方”但不要管这个网址吗?也许您想要做的是使用部分视图来实现这一点?这样,您的基本页面将被路由到,然后在该页面内进行条件测试以显示不同的部分视图?我已经在我的应用程序中这样做了,以查看网格的只读版本或可编辑的网格。它工作得很好。
回答by tvanfosson
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.
然后每个视图将采用模型的特定实例并使用模型呈现其内容。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.
任何时候使用 RedirectResult 时,实际上都会向浏览器发送 HTTP 重定向,这将强制更改 URL。
回答by Jon Norton
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:
我不确定重定向到 .aspx 页面可以做什么,但您应该可以将其替换为RedirectToAction(...)s
以下内容:
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();
}
}
回答by user20358
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:
将 global.asax 文件添加到您的项目。在其中添加以下方法:
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.
GetURLToRedirect 可以是一个具有根据输入的 URL 查找实际 URL 的逻辑的类。 [RedirectURL] 属性将设置为重定向到工作表下方的 URL。
Hope that helps...
希望有帮助...
回答by Selcuk DEMIR
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:
然后你可以像这样在你的视图中使用这些 ViewBags:
@{
Layout = null;
Html.RenderAction(ViewBag.action, ViewBag.controller);
}
That's it. And you can improve this example with use a class and some functions.
就是这样。您可以使用一个类和一些函数来改进这个例子。